返回 登录
0

JavaScript 时间格式化(多种形式)

阅读1468











var timeFormat = {
dateChooseList:{
“year”:function(date){ return date.getFullYear() },
“month”:function(date){ return date.getMonth()+1 },
“day”:function(date){ return date.getDate() },
“hour”:function(date){ return date.getHours() },
“minute”:function(date){ return date.getMinutes() },
“second”:function(date){ return date.getSeconds() }
},
dealChooseTimeFormat:function(choose,date){
return this.dateChooseListchoose;
},
getChooseTimeFormat: function (choose,date) { //传入时间戳
if(typeof(date) === “undefined”){
date = new Date();
return this.dealChooseTimeFormat(choose,date);
}
else{
date = new Date(parseInt(date) * 1000);
if(typeof(this.dateChooseList[choose]) === “function”){
return this.dealChooseTimeFormat(choose,date);
}
else{
return “Error time type!”;
}
}
},
getFullTimeFormat: function (step,date) {
date = date ? new Date(parseInt(date)) : new Date();
step = step || “second”;
var hours = this.dealChooseTimeFormat(“hour”,date);
var minutes = this.dealChooseTimeFormat(“minute”,date);
var seconds = this.dealChooseTimeFormat(“second”,date);
hours = this.saveTwoDigital(hours);
minutes = this.saveTwoDigital(minutes);
seconds = this.saveTwoDigital(seconds);
var newDate = this.dealChooseTimeFormat(“year”,date) + “-” + this.dealChooseTimeFormat(“month”,date) + “-” + this.dealChooseTimeFormat(“day”,date);
switch (step){
case “day”:
return newDate;
break;
case “hour”:
return newDate + ” ” + hours;
break;
case “minute”:
return newDate + ” ” + hours + “:” + minutes;
break;
case “second”:
return newDate + ” ” + hours + “:” + minutes + “:” +seconds;
break;
default :
return “Parameter error!”;
break
}
return newDate;
},
getWeekFormat:function(date){
date = date || new Date();
var today = new Array(‘星期日’,’星期一’,’星期二’,’星期三’,’星期四’,’星期五’,’星期六’);
var week = today[date.getDay()];
return week;
},
countdownUpdate: function (timeIntervals) {
timeIntervals = (Date.parse(new Date()) - timeIntervals)/1000 || 0;
var y = Math.floor(timeIntervals / 60 / 60 / 24 / 30 / 12);
var M = Math.floor(timeIntervals / 60 / 60 / 24 / 30);
var d = Math.floor(timeIntervals / 60 / 60 / 24);
timeIntervals = (timeIntervals - d * 60 * 60 * 24);
var h = Math.floor(timeIntervals / 60 / 60);
var m = Math.floor((timeIntervals - h * 60 * 60) / 60);
var s = Math.floor((timeIntervals - h * 60 * 60 - m * 60));

    if(y>0){
        return y+'年前';
    }
    else if(M>0){
        return M+'月前';
    }
    else if(d>0){
        return d+'天前';
    }
    else if(h>0){
        return h+'小时前';
    }
    else if(m>0){
        return m+'分钟前';
    }
    else{
        return '刚刚';
    }
    //return (d > 0 ? d + ' 天 ' : '') + (h < 10 ? '0' + h : h) + ' 小

时 ’ + (m < 10 ? ‘0’ + m : m) + ’ 分 ’ + (s < 10 ? ‘0’ + s : s) + ’ 秒
‘;
},
saveTwoDigital:function(string){
var len = string.toString().length;
switch(len){
case 1:
string = ‘0’ + string;
break;
case 4:
string = string.toString().substr(2,2);
break;
default :
break
}
return string;
}
};
document.getElementById(“show”).innerText = “今天是:” + timeFormat.getWeekFormat() + “u000au000a”;
document.getElementById(“show”).innerText += “现在是:” + timeFormat.getFullTimeFormat(“second”) + “u000au000a”;
document.getElementById(“show”).innerText += “2016/8/26 2:28:24 是:” + timeFormat.countdownUpdate(“1472149704000”) + “u000au000a”;

图片描述

评论