因为new Date().toLocaleString() 在安卓和ios上的格式不一致,所以尽量自己写format
生成一个时间数组
作为一个简单的举例,我序列化一周的时间对象
声明参数
let lastMonth = []let daylong = 86400000
判断时间值是否小于9
const formatNumber = num => { num = parseInt(num) if (num>9) { return num } else { return '0'+num }}
格式化时间
const formatTime = (time) => { let date = new Date(time) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() return { value: year+'-'+formatNumber(month) + '-' + formatNumber(day), date: month + '月' + day +'日', year: year+'年', select: false }}
循环
for(var i = 1;i<15;i++){ let time = new Date().getTime() + (i+1) * daylong let date = formatTime(time) lastMonth.push(date)}
输出如下:
如果只需要格式化时间
function formatTime(time) { var date = new Date(time) 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, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')}
输出:2019/05/27 14:00:30