日期格式化
<script type
="text/javascript">
Date
.prototype
.Format = function(fmt
) {
var o
= {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math
.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt
))
fmt
= fmt
.replace(RegExp
.$
1, (this.getFullYear() + "").substr(4 - RegExp
.$
1.length
));
for (var k
in o
)
if (new RegExp("(" + k
+ ")").test(fmt
))
fmt
= fmt
.replace(RegExp
.$
1, (RegExp
.$
1.length
== 1) ? (o
[k
]) : (("00" + o
[k
]).substr(("" + o
[k
]).length
)));
return fmt
;
}
alert(new Date().Format("yyyy-MM-dd"));
</script
>
获取时间戳的几种方式以及格式化
<script type
="text/javascript">
var timestamp1
= Date
.parse(new Date());
var timestamp2
= (new Date()).valueOf();
var timestamp3
= new Date().getTime();
console
.log(timestamp2
)
function timetrans(date
) {
var date
= new Date(date
);
var Y = date
.getFullYear() + '-';
var M = (date
.getMonth() + 1 < 10 ? '0' + (date
.getMonth() + 1) : date
.getMonth() + 1) + '-';
var D = (date
.getDate() < 10 ? '0' + (date
.getDate()) : date
.getDate()) + ' ';
var h
= (date
.getHours() < 10 ? '0' + date
.getHours() : date
.getHours()) + ':';
var m
= (date
.getMinutes() < 10 ? '0' + date
.getMinutes() : date
.getMinutes()) + ':';
var s
= (date
.getSeconds() < 10 ? '0' + date
.getSeconds() : date
.getSeconds());
return Y + M + D + h
+ m
+ s
;
}
console
.log(timetrans(1553590469530))
</script
>
时间的前后推迟
function timedelay(now
, d
) {
now
.setHours(10);
now
.setMinutes(0);
now
.setSeconds(0);
now
.setDate(now
.getDate() + d
);
now
= now
.Format("yyyy-MM-dd hh:mm:ss")
return now
;
}
alert(timedelay(new Date(), -1))
alert(timedelay(new Date(), 0))
alert(timedelay(new Date(), 1))
转载请注明原文地址: https://yun.8miu.com/read-134179.html