小程序时间戳转日期,写一个工具类,可以直接调用。
代码:
util.js
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
module.exports = {
formatTime: formatTime
}
在js中的使用:
test.js
const util = require('../../utils/util.js');
Page({
let date = new Date();
let time = util.formatTime(date);
})