getTime 获取某个时间的时间戳-例:new Date() new Date('2001/10/01 08:08:08')
new Date('2001/10/1 08:08:08')传入接口输出2001-10-01T00:08:08.000Z
new Date('2001/10/1 08:08:08')控制台输出Mon Oct 01 2001 08:08:08 GMT+0800 (中国标准时间)
还需要手动处理转为yyyy-MM-dd HH:mm:ss
getYMDHMS = (timestamp = new Date('1970/01/01 00:00:00').getTime()) => {
let time = new Date(timestamp)
let year = time.getFullYear()
const month = (time.getMonth() + 1).toString().padStart(2, '0')
const date = (time.getDate()).toString().padStart(2, '0')
const hours = (time.getHours()).toString().padStart(2, '0')
const minute = (time.getMinutes()).toString().padStart(2, '0')
const second = (time.getSeconds()).toString().padStart(2, '0')
return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
console.log(getYMDHMS())格式
convert = (date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') => {
let _this = date
if (!date) {
return "";
}
var args = {
"M+": _this.getMonth() + 1,
"d+": _this.getDate(),
"h+": _this.getHours(),
"m+": _this.getMinutes(),
"s+": _this.getSeconds(),
"q+": Math.floor((_this.getMonth() + 3) / 3), //quarter
"S": _this.getMilliseconds()
};
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (_this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var i in args) {
var n = args[i];
if (new RegExp("(" + i + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? n : ("00" + n).substr(("" + n).length));
}
return format;
};
console.log(convert())
时间戳转yyyy-MM-dd HH:mm:ss
getTime 获取某个时间的时间戳-例:new Date() new Date('2001/10/01 08:08:08')
getYMDHMS = (timestamp = new Date('1970/01/01 00:00:00').getTime()) => {
let time = new Date(timestamp)
let year = time.getFullYear()
const month = (time.getMonth() + 1).toString().padStart(2, '0')
const date = (time.getDate()).toString().padStart(2, '0')
const hours = (time.getHours()).toString().padStart(2, '0')
const minute = (time.getMinutes()).toString().padStart(2, '0')
const second = (time.getSeconds()).toString().padStart(2, '0')
return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}
console.log(getYMDHMS())
一年有几天
const allDays = (year) => {
let leapYear = false, sum_day = 0, month_arr = [4, 6, 9, 11];
if (year % 100 === 0) { // 年份是整百
leapYear = year % 400 === 0
} else {
leapYear = year % 4 === 0
}
// 下面计算每个月的天数
for (let i = 1; i < 13; i++) {
if (i === 2) {
sum_day += leapYear ? 29 : 28
} else if (month_arr.includes(i)) {
sum_day += 30
} else {
sum_day += 31
}
}
return sum_day
}
console.log(allDays(2005))
待补充........................... 倒计时:服务端返回截至时间-根据当前时间做递减