js 时间戳转日期格式

来源:互联网 发布:unity3d初音炫舞源码 编辑:程序博客网 时间:2024/06/10 07:54

js 时间戳转日期格式


问题描述

后台保存的时间字段是以时间戳格式保存的,前台展现需要转为普通日期格式。

转换函数如下:

// 将时间戳转为普通日期格式function transferTimestamp2DateFormat(timestamp) {    var date = new Date(timestamp);    var year = date.getFullYear();    var month = date.getMonth() + 1;    var day = date.getDate();    var hour = date.getHours();    var minute = date.getMinutes();    var second = date.getSeconds();    return year + "-" + checkTime(month) + "-" + checkTime(day) + " "        + checkTime(hour) + ":" + checkTime(minute) + ":" + checkTime(second);}// 补0function checkTime(time) {    if (time < 10) {        time = "0" + time;    }    return time;}

该方法返回的格式如下:
2017-09-09 22:22:22




原创粉丝点击