js时间格式化

Posted 凉梁凉糕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js时间格式化相关的知识,希望对你有一定的参考价值。

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时间格式化的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Javadoc 中使用 @ 和 符号格式化代码片段?

为 Blogger 上的博客格式化代码片段 [关闭]

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

Node.js JavaScript 片段中的跳过代码