獲取今天的時間
new Date()
時間的修改操作:
1.修改時區的差異
以今天時間為基准獲取前八個小時的時間語法:
new Date((new Date().getTime() - 8* 60 * 60 * 1000)) //就是修改時區的差異(比如北京時區與成都時區的差異)
2.獲取年
new Date().getFullYear()
3.獲取月
注意!!!:
getMonth()這個方法獲取月是從0月開始計數的(只有這個調用方法存在點不同)
比如下方的運行結果,這個月應該是12月.但顯示結果為11月,如果想正確顯示,在表達式后面加1即可
new Date().getMonth()
new Date().getMonth()+1
4.獲取天
new Date().getDate()
5.獲取對應的星期幾
new Date().getDay()
6.獲取年月日(有很多種方法)
new Date().toDateString()
這邊直接列舉
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 (中國標准時間)'
7.獲取毫秒級的(一般用不上)
new Date().getMilliseconds()
8.getTime的方法
new Date().getTime()
//getTime() 方法返回一個時間的格林威治時間數值
//這個數值:表示從1970年1月1日0時0分0秒(UTC,即協調世界時)距離該日期對象所代表時間的毫秒數。