使用js獲取當(dāng)前年月日的方法及格式整理匯總

1. 獲取完整時間戳
var date = new Date(); ====》 Wed Sep 07 2022 17:18:30 GMT+0800 (中國標準時間) 小節(jié)2中的數(shù)據(jù)以此為基礎(chǔ)
2. 整理年月日數(shù)據(jù)為: 2022-02-09 的格式
var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); month = (month > 9) ? month : (“0” + month); day = (day < 10) ? (“0” + day) : day; var today = year + “-” + month + “-” + day;
3. 關(guān)于日期數(shù)據(jù)的額外補充
date.getYear(); // 獲取當(dāng)前年份(2 位)
date.getFullYear(); // 獲取完整的年份(4 位, 1970-???)
date.getMonth(); // 獲取當(dāng)前月份(0-11,0 代表 1 月)
date.getDate(); // 獲取當(dāng)前日(1-31)
date.getDay(); // 獲取當(dāng)前星期 X(0-6,0 代表星期天)
date.getTime(); // 獲取當(dāng)前時間(從 1970.1.1 開始的毫秒數(shù))
date.getHours(); // 獲取當(dāng)前小時數(shù)(0-23)
date.getMinutes(); // 獲取當(dāng)前分鐘數(shù)(0-59)
date.getSeconds(); // 獲取當(dāng)前秒數(shù)(0-59)
date.getMilliseconds(); // 獲取當(dāng)前毫秒數(shù)(0-999)
date.toLocaleDateString(); // 獲取當(dāng)前日期
date.toLocaleTimeString(); // 獲取當(dāng)前時間
date.toLocaleString( ); // 獲取日期與時間
4. 判斷閏年
var date1 = new Date();
Date.prototype.isLeapYear = function() {
return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
}
console.log(date1.isLeapYear()); // false
5.日期格式化
/** 格式 YYYY/yyyy/YY/yy 表示年份
* MM/M 月份
* W/w 星期
* dd/DD/d/D 日期
* hh/HH/h/H 時間
* mm/m 分鐘
* ss/SS/s/S 秒
**/
//---------------------------------------------------
Date.prototype.Format = function(formatStr) {
var str = formatStr;
var Week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + (this.getMonth()+1));
str=str.replace(/M/g,this.getMonth());
str=str.replace(/w|W/g,Week[this.getDay()]);
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
str=str.replace(/d|D/g,this.getDate());
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes());
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds());
return str;
}
var date=new Date();
var res=date.Format("yyyy-MM-dd HH:mm:ss 星期W");
console.info(res); //2022-01-07 16:18:36 星期五總結(jié)
到此這篇關(guān)于使用js獲取當(dāng)前年月日的方法及格式整理匯總的文章就介紹到這了,更多相關(guān)js獲取當(dāng)前年月日及格式整理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端圖片懶加載(lazyload)的實現(xiàn)方法(提高用戶體驗)
圖片懶加載又稱圖片延時加載、惰性加載,即在用戶需要使用圖片的時候加載,這樣可以減少請求,節(jié)省帶寬,提高頁面加載速度,相對的,也能減少服務(wù)器壓力,下面通過本文給大家分享圖片懶加載lazyload的實現(xiàn)方法,感興趣的朋友一起看看吧2017-08-08
JavaScript實現(xiàn)合并(歸并)排序算法示例解析
這篇文章主要為大家介紹了JavaScript實現(xiàn)合并(歸并)排序算法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
JS實現(xiàn)獲取來自百度,Google,soso,sogou關(guān)鍵詞的方法
這篇文章主要介紹了JS實現(xiàn)獲取來自百度,Google,soso,sogou關(guān)鍵詞的方法,結(jié)合實例形式分析了js獲取來路頁面的方法與相關(guān)搜索引擎關(guān)鍵詞的處理技巧,需要的朋友可以參考下2016-12-12
javascript游戲開發(fā)之《三國志曹操傳》零部件開發(fā)(一)讓靜態(tài)人物動起來
首先來說,讓一個游戲賦有可玩性必須要動靜結(jié)合,我將要在下面告訴大家如何運用Javascript將靜態(tài)圖片變?yōu)閯討B(tài)圖片,感興趣的朋友可以了解下,便當(dāng)鞏固js知識了2013-01-01
使用JavaScript將富文本HTML轉(zhuǎn)換為純文本的三種方法
在Web開發(fā)中,我們經(jīng)常需要處理HTML內(nèi)容,但有時為了特定的目的,比如文本處理、搜索或顯示在非HTML環(huán)境中,我們可能希望將富文本HTML轉(zhuǎn)換為純文本,這里,我們將探討如何使用JavaScript來實現(xiàn)這一功能,需要的朋友可以參考下2024-05-05
JavaScript+CSS實現(xiàn)仿天貓側(cè)邊網(wǎng)頁菜單效果
這篇文章主要介紹了JavaScript+CSS實現(xiàn)仿天貓側(cè)邊網(wǎng)頁菜單效果,涉及javascript鼠標事件及頁面元素動態(tài)操作的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

