1.獲取當(dāng)前時(shí)間
var myDate = new Date();
2.獲取時(shí)間中的年月日時(shí)分秒
myDate.getYear(); // 獲取當(dāng)前年份(2位)
myDate.getFullYear(); // 獲取完整的年份(4位,1970-????)
myDate.getMonth(); // 獲取當(dāng)前月份(0-11,0代表1月)
myDate.getDate(); // 獲取當(dāng)前日(1-31)
myDate.getDay(); // 獲取當(dāng)前星期X(0-6,0代表星期天)
myDate.getTime(); // 獲取當(dāng)前時(shí)間(從1970.1.1開始的毫秒數(shù))
myDate.getHours(); // 獲取當(dāng)前小時(shí)數(shù)(0-23)
myDate.getMinutes(); // 獲取當(dāng)前分鐘數(shù)(0-59)
myDate.getSeconds(); // 獲取當(dāng)前秒數(shù)(0-59)
myDate.getMilliseconds(); // 獲取當(dāng)前毫秒數(shù)(0-999)
myDate.toLocaleDateString(); // 獲取當(dāng)前日期
var mytime=myDate.toLocaleTimeString(); // 獲取當(dāng)前時(shí)間
myDate.toLocaleString( ); // 獲取日期與時(shí)間
3.時(shí)間的格式化
// 對Date的擴(kuò)展寝受,將 Date 轉(zhuǎn)化為指定格式的String
// 月(M)滥比、日(d)馆衔、小時(shí)(h)椎瘟、分(m)未桥、秒(s)栽渴、季度(q) 可以用 1-2 個(gè)占位符,
// 年(y)可以用 1-4 個(gè)占位符囱持,毫秒(S)只能用 1 個(gè)占位符(是 1-3 位的數(shù)字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { // author: meizz
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小時(shí)
"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 (var 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;
}
作者:折戟沉沙_wx
來源:CSDN
原文:https://blog.csdn.net/vasilis_1/article/details/73649961