JS獲取日期及修改日期方法匯總


JS

獲取今天的時間

 new Date()

image-20211216090044827

時間的修改操作:

1.修改時區的差異

以今天時間為基准獲取前八個小時的時間語法:

   new Date((new Date().getTime() - 8* 60 * 60 * 1000))   //就是修改時區的差異(比如北京時區與成都時區的差異)

image-20211216090434760

2.獲取年

 new Date().getFullYear()

image-20211216090701772

3.獲取月

注意!!!:

getMonth()這個方法獲取月是從0月開始計數的(只有這個調用方法存在點不同)

比如下方的運行結果,這個月應該是12月.但顯示結果為11月,如果想正確顯示,在表達式后面加1即可

 new Date().getMonth()

image-20211216090814020

 new Date().getMonth()+1

image-20211216091230217

4.獲取天

 new Date().getDate()

image-20211216091438295

5.獲取對應的星期幾

 new Date().getDay()

image-20211216091711270

6.獲取年月日(有很多種方法)

 new Date().toDateString()

image-20211216092233780

這邊直接列舉

 new Date().toLocaleDateString()
 //'2021/12/16'
 new Date().toLocaleString()
 //'2021/12/16 上午9:23:52'
 new Date().toLocaleTimeString()
 //'上午9:24:05'
 new Date().toGMTString()
 //'Thu, 16 Dec 2021 01:24:20 GMT'
 new Date().toISOString()
 //'2021-12-16T01:24:32.948Z'
 new Date().toISOString()
 //'2021-12-16T01:24:46.427Z'
 new Date().toJSON()
 //'2021-12-16T01:25:08.370Z'
 new Date().toTimeString()
 //'09:25:21 GMT+0800 (中國標准時間)'
 new Date().toString()
 //'Thu Dec 16 2021 09:25:35 GMT+0800 (中國標准時間)'

 

image-20211216092637310

7.獲取毫秒級的(一般用不上)

 new Date().getMilliseconds()

image-20211216093006149

 

8.getTime的方法

 new Date().getTime()

image-20211216093508269

 //getTime() 方法返回一個時間的格林威治時間數值
 //這個數值:表示從1970年1月1日0時0分0秒(UTC,即協調世界時)距離該日期對象所代表時間的毫秒數。
 

 

9.若有不足,請聯系補充完成

img

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM