JavaScript獲取當(dāng)前時間戳5種方法匯總
更新時間:2023年10月08日 09:03:42 作者:TTong___
很多時候我們都把時間戳作為id值,下面這篇文章主要給大家介紹了關(guān)于JavaScript獲取當(dāng)前時間戳的5種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
js 獲取當(dāng)前時間戳的5種方法
1. 獲取時間戳精確到 秒,13位
const timestamp = Date.parse(new Date()); console.log(timestamp); //輸出 1591669256000 13位
2. 獲取時間戳精確到 毫秒,13位
const timestamp = Math.round(new Date()); console.log(timestamp); //輸出 1591669961203 13位
3. 獲取時間戳精確到 毫秒,13位
const timestamp = (new Date()).valueOf(); console.log(timestamp); //輸出 1591670037603 13位
4. 獲取時間戳精確到 毫秒,13位
const timestamp = new Date().getTime(); console.log(timestamp); //輸出 1591670068833 13位
5. 獲取時間戳精確到 毫秒,13位
const timestamp = +new Date(); console.log(timestamp); //輸出 1591670099066 13位
js 獲取今日、本周、本月、本年 時間戳的方法
1. 今日時間
// 今天開始時間的時間戳 new Date(new Date().toLocaleDateString()).getTime() // 今天結(jié)束時間的時間戳 new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1
2. 本周時間
// 本周開始時間的時間戳 new Date(new Date().setHours(0, 0, 0) - (new Date().getDay() - 1) *24 * 60 * 60 *1000) // 本周結(jié)束時間的時間戳 new Date(new Date().setHours(0, 0, 0) + (7 - new Date().getDay()) *24 * 60 * 60 *1000)
3. 本月時間
// 本月開始時間的時間戳 new Date(new Date(new Date().getFullYear(), new Date().getMonth(), 1).setHours(0, 0, 0)) // 本月結(jié)束時間的時間戳 new Date(new Date(new Date().getFullYear(), new Date().getMonth()+ 1, 0).setHours(23, 59, 59, 59))
4. 本年時間
// 本年開始時間的時間戳 new Date(new Date().getFullYear(), 0, 1) // 本年結(jié)束時間的時間戳 new Date(new Date(new Date().getFullYear() + 1, 0, 0).setHours(23, 59, 59, 59))
總結(jié)
到此這篇關(guān)于JavaScript獲取當(dāng)前時間戳5種方法的文章就介紹到這了,更多相關(guān)JS獲取當(dāng)前時間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript中檢測數(shù)據(jù)類型的四種方法
這篇文章主要給大家分享的是JavaScript中檢測數(shù)據(jù)類型的四種方法,有 typeof、instanceof、constructor、Object.prototype.toString.call(),下面文章詳細(xì)介紹內(nèi)容,需要的小伙伴可以參考一下2022-01-01
前端JS實(shí)現(xiàn)瀏覽器跨標(biāo)簽頁數(shù)據(jù)共享的五大方案
這篇文章主要為大家詳細(xì)介紹了五大常見的瀏覽器跨頁簽數(shù)據(jù)共享方案,包括它們的實(shí)現(xiàn)原理、優(yōu)缺點(diǎn)以及適用場景,有需要的小伙伴可以跟隨小編一起參考一下2026-02-02
酷! 不同風(fēng)格頁面布局幻燈片特效js實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了超酷不同風(fēng)格頁面布局幻燈片特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
createTextRange()的使用示例含文本框選中部分文字內(nèi)容
這篇文章主要介紹了createTextRange()的使用示例,需要的朋友可以參考下2014-02-02
微信小程序購物商城系統(tǒng)開發(fā)系列-目錄結(jié)構(gòu)介紹
這篇文章主要介紹了微信小程序購物商城系統(tǒng)開發(fā)系列-目錄結(jié)構(gòu)介紹,有興趣的可以了解一下。2016-11-11

