码迷,mamicode.com
首页 > Web开发 > 详细

js时间格式化

时间:2017-12-01 17:41:40      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:日期   fun   format   格式   day   style   gettime   pre   turn   

1 当给定时间为1997-10-10时转化为毫秒值,需要先提取这样的参数,在使用getTime

2 当给定的值为毫秒值 转化为日期显示时 

  function formatBeginTime(time){
    var year = time.getFullYear();
    var mouth = time.getMonth();//比实际只小,此处若加1下面新建时间将对应不上
    var day = time.getDate();
    var temp = new Date(year,mouth,day,00,00,00);
    return temp;
  }

3 格式化时间,当小于一天时小时小时及分钟,大于时显示日期

function convertTime(tem){
    var curDate = new Date();
    var numEng = [‘one‘,‘two‘,‘three‘,‘four‘,‘five‘,‘six‘,‘seven‘,‘eight‘,‘nine‘,‘ten‘,‘eleven‘,‘twelve‘,‘thirteen‘,‘fourteen‘,‘fifteen‘,‘sixteen‘,‘seventeen‘,‘eighteen‘,‘nineteen‘,‘twenty‘,‘twenty-one‘,‘twenty-two‘,‘twenty-three‘,‘twenty-four‘,‘twenty-five‘,‘twenty-six‘,‘twenty-seven‘,‘twenty-eight‘,‘twenty-nine‘,‘thirty‘,‘thirty-one‘,‘thirty-two‘,‘thirty-three‘,‘thirty-four‘,‘thirty-five‘,‘thirty-six‘,‘thirty-seven‘,‘thirty-eight‘,‘thirty-nine‘,‘forty‘,‘forty-one ‘,‘forty-two‘,‘forty-three‘,‘ forty-four‘,‘forty-five‘,‘ forty-six‘,‘ forty-seven‘,‘ forty-eight‘,‘ forty-nine‘,‘ fifty‘,‘fifty-one‘,‘fifty-two‘,‘fifty-three‘,‘fifty-four ‘,‘fifty-five‘,‘fifty-six‘,‘fifty-seven‘,‘fifty-eight‘,‘fifty-nine‘,‘sixty‘];
    var diff =  curDate.getTime()-tem;
    if(diff<3600000){
        var minute = Math.ceil(diff/60000);
        minute = numEng[minute-1];
        tem = minute+‘&nbsp;minutes ago‘;
    }else if(diff<86400000){
        var hour = Math.ceil(diff/3600000);
        hour = numEng[hour-1]
        tem = hour+‘&nbsp;hours ago‘;
    }else{
        var date = dateFormat(tem);
        tem = date;
    }
    return tem;
}
function dateFormat(time){
    var time =  new Date(time);
    var year = time.getFullYear();
    var month = time.getMonth()+1;
    var day = time.getDate();
    function zeroFill(t){
        if(t<10){
            return ‘0‘+t;
        }else{
            return t;
        }

    }
    return year+"-"+zeroFill(month)+"-"+zeroFill(day);
}

 

js时间格式化

标签:日期   fun   format   格式   day   style   gettime   pre   turn   

原文地址:http://www.cnblogs.com/xhliang/p/7943559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!