自己写了一个小项目,需要利用JS将标准的UNIX时间戳转成普通的日期时间来便于访客的查看。下面提供了两种方法,各位看着用吧。
js将unix时间戳转成普通日期时间的方法
方法1:
js代码:
// var t = 1640417486; var tims = new Date(t*1000); var timeform = tims.toLocaleString() console.log(timeform);
打印结果:
2021/12/25 下午3:31:26
方法2:
定义一个Date中的方法函数!
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;
};直接调用函数:
var t = 1640417486;
var tims = new Date(t*1000).Format("yyyy-MM-dd hh:mm:ss");
console.log(tims);
// 2021-12-25 15:31:26
var tims = new Date(t*1000).Format("yyyy-MM-dd hh:mm");
console.log(tims);
// 2021-12-25 15:31 以上就是javascript将Unix时间戳转为普通日期时间的方法。交际的目的就是为了能与更多的人进行交往,能够更快地提升自己,能够获得别人更多的帮助。而要想让别人喜欢与你交往,就要树立自己的良好形象,用形象采打造出成功的交际。更多关于javascript将Unix时间戳转为普通日期时间的方法请关注haodaima.com其它相关文章!



![一个AJAX自动完成功能的js封装源码[支持中文]第1/2页](http://files.haodaima.com/upload/2007228153419399.gif)