微信小程序中格式化时间的代码实现
本篇文章给大家带来的内容是关于微信小程序中格式化时间的代码实现 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、 formatTime :Data型 2.formatNumber1:Long型util.js----------//两种方式 1.formatTime 传入参数 Date 返回:年/月/日const formatTime = date => {const year = date.getFullYear()const month = date.getMonth() + 1const 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以上就是微信小程序中格式化时间的代码实现的详细内容,更多请关注小潘博客其它相关文章!