util.js
/**
* 时间戳转化为年 月 日 时 分 秒
* ts: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
*/
const 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;
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
tsFormatTime: tsFormatTime,
}
.js
// pages/achievement/achievement.js
import Util from "../../utils/util.js"
Page({
currentDate: new Date().getTime(),
minDate: new Date().getTime(),
currenttime: '',
time: '',
year: '',
month: ''
},
onLoad: function(options) {
this.setData({
currenttime: Util.tsFormatTime(new Date().getTime(), 'Y年M月'),
time: Util.tsFormatTime(new Date().getTime(), 'Y-M')
})
console.log(this.data.time)
var arr = [];
arr = this.data.time.split("-");
console.log(arr)
this.setData({
year: arr[0],
month: arr[1]
})
},
})