1.计算纯日期 (条件:已知相对于1900-01-01过去的天数)
var startTime = this._getOriginalTimeMs();
var currentValue = parseInt(value) + 1 ;
var lastTime = startTime + currentValue * 24 * 60 * 60 * 1000;
var showTime = new Date(lastTime);
_getOriginalTimeMs () {
var originalTime = new Date(1900, 0, 1, 0, 0, 0);
return originalTime.getTime();//得到毫秒数
}
2.计算纯时间 (条件:已知相对于00:00过去的秒数)
//实现的逻辑就是拿到当天的0点0分0秒的时间zeroTime,
//然后拿要计算的秒数value变成毫秒之后加上zeroTime变成的毫秒进行计算
var zeroTime = new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate() , '00','00','00');
var time = new Date(zeroTime.getTime() + parseInt(value) * 1000);
var time1 = this._addZero(time.getHours()) + ":" + this._addZero(time.getMinutes());
//给日期加"0"
_addZero(obj) {
if (parseInt(obj) < 10) {
return "0" + '' + obj;
} else {
return obj;
}
},
比如:value:32580
得出的结果: time:Mon Sep 25 2017 09:03:00 GMT+0800 (CST)
3.将GMT+0800 (CST)格式的时间转化为标准的XML Schema的"日期型数据格式”
//要转化的时间是timeValue
var currentDate = timeValue.toJSON();
比如Thu Sep 21 2017 09:32:00 GMT+0800 (CST)
转化为'2017-09-21T01:32:00.000Z'
4.计算几年的时间(包括每个月应该是几天)
_createDateData() {
let date = [];
let datetime = new Date();
let currentYear = parseInt(datetime.getFullYear());
for (let i = currentYear; i < currentYear + 10 ; i++) {
let month = [];
for (let j = 1; j < 13; j++) {
let day = [];
if (j === 2) {
for (let k = 1; k < 29; k++) {
day.push(k);
}
//Leap day for years that are divisible by 4, such as 2000, 2004
if (i % 4 === 0) {
day.push(29);
}
}
else if (j in {1: 1, 3: 1, 5: 1, 7: 1, 8: 1, 10: 1, 12: 1}) {
for (let k = 1; k < 32; k++) {
day.push(k);
}
}
else {
for (let k = 1; k < 31; k++) {
day.push(k);
}
}
let _month = {};
var s = Months[j - 1];
_month[s] = day;
month.push(_month);
}
let _date = {};
_date[i] = month;
date.push(_date);
}
return date;
}
如果有不对的地方请多指教。
网友评论