- 获取当前时间(如:Fri Dec 02 2022 09:20:38 GMT+0800 (中国标准时间))
let date = new Date();
- 获取年、月、日、星期、时、分、秒
let myYear = date.getFullYear();
let myMonth = date.getMonth() + 1;
let myToday = date.getDate();
let myDay = date.getDay();
let myHours = date.getHours();
let myMinutes = date.getMinutes();
let mySeconds = date.getSeconds();
myMonth = myMonth > 9 ? myMonth : '0' + myMonth
myToday = myToday > 9 ? myToday : '0' + myToday
let nowDate = myYear + '年' + myMonth + '月' + myToday + '日'
const weeks = new Array(
"周日",
"周一",
"周二",
"周三",
"周四",
"周五",
"周六"
);
let myWeek = weeks[myDay]
nowDate = nowDate + ' ' + myWeek
- 更多
date.getTime();
date.getMilliseconds();
date.toLocaleDateString();
date.toLocaleTimeString();
date.toLocaleString( );
- 时间格式化
Date.prototype.Format = function (fmt) {
let o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
let YMD_time = new Date().Format("yyyy-MM-dd");
let YMDHMS_time = new Date().Format("yyyy-MM-dd hh:mm:ss");
- 时间格式化 - 在vue框架中转换时间戳,可见这篇文章