直接上js ,你可以单独保存在一个js文件中,当要用的时候直接饮用即可
//日期格式化年月日时分秒函数
var formatTimeToDate = function(time){
return new Date(time).format("yyyy-MM-dd hh:mm");
};
//日期格式化年月日时
var formatTimeToDay = function(time){
return new Date(time).format("yyyy-MM-dd");
};
Date.prototype.format = function(format) {
var date = {
"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+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
};
html中使用,根据不同的场景来调用,总之帮时间戳放到函数中就完事
formatTimeToDate(时间戳); 2018-07-21 15:00
formatTimeToDay(时间);2018-07-21
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>时间戳转化函数</title>
<script src="js/public.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
</body>
<script type="text/javascript">
alert(formatTimeToDate(1532156400000)) //2018-07-21 15:00
alert(formatTimeToDay(1532156400000))//2018-07-21
</script>
</html>