使用js獲取當(dāng)前年月日的方法及格式整理匯總
1. 獲取完整時(shí)間戳
var date = new Date(); ====》 Wed Sep 07 2022 17:18:30 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間) 小節(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ù)的額外補(bǔ)充
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)前時(shí)間(從 1970.1.1 開(kāi)始的毫秒數(shù))
date.getHours(); // 獲取當(dāng)前小時(shí)數(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)前時(shí)間
date.toLocaleString( ); // 獲取日期與時(shí)間
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 時(shí)間 * 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp發(fā)送formdata表單請(qǐng)求2種方法(全網(wǎng)最簡(jiǎn)單方法)
這篇文章主要給大家介紹了關(guān)于uniapp發(fā)送formdata表單請(qǐng)求2種方法的相關(guān)資料,本文介紹的方法應(yīng)該是全網(wǎng)最簡(jiǎn)單方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09JS動(dòng)態(tài)調(diào)用方法名示例介紹
在JS中如何動(dòng)態(tài)調(diào)用方法名,想必很多的朋友們都不會(huì)吧,下面為大家舉例介紹下具體的調(diào)用方法2013-12-12JavaScript不刷新實(shí)現(xiàn)瀏覽器的前進(jìn)后退功能
這篇文章主要介紹了JavaScript不刷新實(shí)現(xiàn)瀏覽器的前進(jìn)后退功能,本文給出了HTML5解決方案、老舊瀏覽器的寫(xiě)法等方法,需要的朋友可以參考下2014-11-11用原生JS實(shí)現(xiàn)愛(ài)奇藝首頁(yè)導(dǎo)航欄代碼實(shí)例
這篇文章主要介紹了用原生JS實(shí)現(xiàn)愛(ài)奇藝首頁(yè)導(dǎo)航欄代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09JS實(shí)現(xiàn)監(jiān)控微信小程序的原理
這篇文章主要介紹了JS實(shí)現(xiàn)監(jiān)控微信小程序的原理,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06laydate如何根據(jù)開(kāi)始時(shí)間或者結(jié)束時(shí)間限制范圍
這篇文章主要為大家詳細(xì)介紹了laydate根據(jù)開(kāi)始時(shí)間或者結(jié)束時(shí)間限制范圍的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11使用JavaScript實(shí)現(xiàn)頁(yè)面局部更新的方法總結(jié)
在JavaScript中,Ajax(Asynchronous JavaScript and XML)是一種用于在后臺(tái)與服務(wù)器進(jìn)行異步通信的技術(shù),本文給大家介紹了使用JavaScript實(shí)現(xiàn)頁(yè)面局部更新的三種方法,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12