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











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”;

图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值