wchat小程序中的日期格式化
1.微信自带的
const format=require("../../utils/util.js");
var date = format.formatTime(this.data.expirationDate,'Y/M/D');
2.把2017/8/1 0:00:00转换成 2017-08-01
1.util.js里面的代码
const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } /** * 时间戳转化为年 月 日 时 分 秒 * ts: 传入时间戳 * format:返回格式,支持自定义,但参数必须与formateArr里保持一致 */ function tsFormatTime(timestamp, format) { const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']; let returnArr = []; let date = new Date(timestamp); let year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() let hour = date.getHours() let minute = date.getMinutes() let second = date.getSeconds() returnArr.push(year, month, day, hour, minute, second); returnArr = returnArr.map(formatNumber); for (var i in returnArr) { format = format.replace(formateArr[i], returnArr[i]); } return format; } module.exports = { tsFormatTime: tsFormatTime }
2.var util = require('../../utils/util.js'); 3.把日期转换成时间戳 formatTimeStamp:function(data){ return Date.parse(new Date(`${data}`)) || Date.parse(new Date(`${data.replace(/-/g, '/')}`)) }, 4. var date = this.formatTimeStamp(curUserInfor.BIRTHDAY); var newDate = util.tsFormatTime(date, 'Y-M-D');