1. 获取当前时间 一周前 一月前
毫秒数
let now = new Date().getTime()
let dayAgo = new Date().getTime() - 3600 * 1000 * 24 * 7
let monthAgo = new Date().getTime() - 3600 * 1000 * 24 * 30
//转化秒
function formatTimeStamp(data) {
let d = new Date(data);
return Math.round(d.getTime() / 1000);
}
//秒转时分秒
function formatTime(seconds) {
return [
parseInt(seconds / 60 / 60),
parseInt((seconds / 60) % 60),
parseInt(seconds % 60),
].join(":").replace(/\b(\d)\b/g, "0$1");
}
/** yymmddhhmmss(new Date) 2018-07-23 14:55:55 */
function yymmddhhmmss(date_obj = {}, delimiter = "-") {
const yy = date_obj.getFullYear().toString();
const mm = (date_obj.getMonth() + 1).toString();
const dd = date_obj.getDate().toString();
const hh = date_obj.getHours().toString();
const MM = date_obj.getMinutes().toString();
const ss = date_obj.getSeconds().toString();
return (
yy +
delimiter +
(mm[1] ? mm : `0${mm[0]}`) +
delimiter +
(dd[1] ? dd : `0${dd[0]}`) +
" " +
(hh[1] ? hh : `0${hh[0]}`) +
":" +
(MM[1] ? MM : `0${MM[0]}`) +
":" +
(ss[1] ? ss : `0${ss[0]}`)
);
}
console.log('毫秒-------------------')
console.log('当前时间毫秒--', now)
console.log('一周前时间毫秒--', dayAgo)
console.log('一月前时间毫秒--', monthAgo)
console.log('秒---------------------')
console.log('当前时间秒--', formatTimeStamp(now))
console.log('一周前时间秒--', formatTimeStamp(dayAgo))
console.log('一月前时间秒--', formatTimeStamp(monthAgo))
console.log('时:分:秒---------------------')
console.log('当前时分秒--', formatTime(now))
console.log('一周前时分秒--', formatTime(dayAgo))
console.log('一月前时分秒--', formatTime(monthAgo))
console.log('年-月-日 时:分:秒---------------------')
console.log('当前时间--',yymmddhhmmss(new Date(now)))
console.log('一周前--',yymmddhhmmss(new Date(dayAgo)))
console.log('一月前--',yymmddhhmmss(new Date(monthAgo)))