所属分类:php教程
1、 formatTime :Data型 2.formatNumber1:Long型
util.js
----------
//两种方式 1.formatTime 传入参数 Date 返回:年/月/日
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(':')
}const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime,
formatTime1: formatTime1
}
function formatTime1(n){
n = n.toString()
return n[1] ? n : '0' + n
}
// 2.formatTime1传入参数 (时间戳,格式:如YYYY -MM-DD)
//返回:年-月-日(自定义)
function formatTime1(number, format) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = [];
var date = new Date(number);
returnArr.push(date.getFullYear());
returnArr.push(formatNumber1(date.getMonth() + 1));
returnArr.push(formatNumber1(date.getDate()));
returnArr.push(formatNumber1(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber1(date.getSeconds()));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
}
登录后复制
相关推荐:
JSON格式化与serialize序列化 - 璐小璐
php格式化工具Beautify PHP小小BUG
以上就是微信小程序中格式化时间的代码实现的详细内容,更多请关注zzsucai.com其它相关文章!