1. 程式人生 > >js時間格式化

js時間格式化

定時 ive six form pre span style 提取 zerofill

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時間格式化