Date时间

[[toc]]

// 时间戳
Date.now()
new Date().getTime();
new Date().valueOf()

// 设置时间
new Date("2019-06-16 07:55:55"); // Sun Jun 16 2019 07:55:55 GMT+0800 (中国标准时间)

// 时间格式化
date2 = new Date().toISOString().slice(0,10); // 2019-06-16

typeof date2 // 'object'

时间格式化

// 补0
function fillZero(num) {
return num >= 10 ? num : "0" + num;
}

// 方法一
function format(timestamp, formatStr) {
if (!timestamp) return undefined
let date = new Date(timestamp)
let year = date.getFullYear()
let month = this.fillZero(date.getMonth() + 1)
let day = this.fillZero(date.getDate())
let hour = this.fillZero(date.getHours())
let minute = this.fillZero(date.getMinutes())
let second = this.fillZero(date.getSeconds())

return formatStr
.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hour)
.replace('mm', minute)
.replace('ss', second)
}
// 方法二
function format(timeStamp) {
let date = new Date(timeStamp);
return date.getFullYear() + "年"
+ (date.getMonth() + 1).toString().padStart(2, 0) + "月"
+ (date.getDate()).toString().padStart(2, 0) + "日 "
+ (date.getHours()).toString().padStart(2, 0) + ":"
+ (date.getMinutes().toString().padStart(2, 0));
}

倒计时时间格式化

function format_time(timeStamp) {
let day = Math.floor(timeStamp / (24 * 3600 * 1000));
let leave1 = timeStamp % (24 * 3600 * 1000);
let hours = Math.floor(leave1 / (3600 * 1000));
let leave2 = leave1 % (3600 * 1000);
let minutes = Math.floor(leave2 / (60 * 1000));
let leave3 = leave2 % (60 * 1000);
let seconds = Math.floor(leave3 / 1000);
if (day) return day + "天" + hours + "小时" + minutes + "分";
if (hours) return hours + "小时" + minutes + "分" + seconds + "秒";
if (minutes) return minutes + "分" + seconds + "秒";
if (seconds) return seconds + "秒";
return "时间到!";
}

距离现在多久

const SECOND = 1000
const MINUTE = SECOND * 60
const HOUR = MINUTE * 60
const DAY = HOUR * 24

function formatToInterval(timestamp) {
let now = Date.now()
let value = now - timestamp
value = value < 1 ? 1 : value

if (value < MINUTE) {
return Math.floor(value / SECOND) + '秒前'
}
if (value < HOUR) {
return Math.floor(value / MINUTE) + '分钟前'
}
if (value < DAY) {
return Math.floor(value / HOUR) + '小时前'
}
return format(timestamp, 'MM月DD日')
}