getWhatDate: function (str) { //有参数传递为起始时间,无参数传递为结束时间
// 获取时间戳和数据周期 // 开始时间,格式yyyy - MM - dd HH: mm: ss
var nowdate = new Date();
var halfYearDate = new Date(Date.now() - (365 / 2 * 24 * 3600 * 1000));
if (str) {
timeDate = getdate(halfYearDate, str)
} else {
timeDate = getdate(nowdate)
}
function getdate(date, str) {
// Y = date.getFullYear() + '-';
// M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
// D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
// h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
// m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
// s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
Y = date.getFullYear();
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
if (str) {
var M = parseInt(M);
if (M < 9) {
return Y + '-' + '0' + (M + 1) + '-' + '01 00:00:00';
} else if (M == 9) {
return Y + '-' + '10-01 00:00:00';
} else if (M == 10) {
return Y + '-' + '11-01 00:00:00';
} else if (M == 11) {
return Y + '-' + '12-01 00:00:00';
} else if (M == 12) {
return (Y + 1) + '-' + '01-01 00:00:00';
}
} else {
return Y + '-' + M + '-' + D + ' 23:59:59';
}
}
return timeDate
},
var time = getWhatDate('start')
获取开始和结束日期时间(半年周期)
最新推荐文章于 2022-09-14 09:13:01 发布