JavaScript時(shí)間戳與時(shí)間的轉(zhuǎn)化常用方法
在 JavaScript 中,時(shí)間戳(Timestamp)通常指 Unix 時(shí)間戳,即從 1970年1月1日 00:00:00 UTC 到某個(gè)時(shí)間點(diǎn)經(jīng)過(guò)的 毫秒數(shù)(注意:其他語(yǔ)言如 Python 可能使用秒,但 JavaScript 默認(rèn)用毫秒)。以下是時(shí)間戳與時(shí)間格式相互轉(zhuǎn)換的常用方法:
1. 獲取當(dāng)前時(shí)間戳
// 方法1:Date.now() const timestamp1 = Date.now(); // 方法2:new Date().getTime() const timestamp2 = new Date().getTime(); // 方法3:+new Date() const timestamp3 = +new Date();
2. 時(shí)間戳 → 時(shí)間對(duì)象
用時(shí)間戳生成 Date 對(duì)象后,可提取具體時(shí)間信息:
const timestamp = 1696147200000; // 示例時(shí)間戳(2023-10-01 00:00:00 UTC) const date = new Date(timestamp); // 提取時(shí)間信息(本地時(shí)區(qū)) const year = date.getFullYear(); // 2023 const month = date.getMonth() + 1; // 10(注意月份從0開(kāi)始,需+1) const day = date.getDate(); // 1 const hours = date.getHours(); // 8(假設(shè)時(shí)區(qū)為UTC+8) const minutes = date.getMinutes(); // 0 const seconds = date.getSeconds(); // 0 // 提取UTC時(shí)間信息 const utcHours = date.getUTCHours(); // 0(UTC時(shí)間)
3. 時(shí)間戳 → 格式化字符串
將 Date 對(duì)象格式化為易讀的字符串:
// 方法1:使用內(nèi)置方法(本地時(shí)區(qū))
const localDateStr = date.toLocaleDateString(); // "2023/10/1"
const localTimeStr = date.toLocaleTimeString(); // "08:00:00"
const localStr = date.toLocaleString(); // "2023/10/1 08:00:00"
// 方法2:手動(dòng)拼接(靈活定制)
const formattedTime = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours}:${minutes}:${seconds}`;
// "2023-10-01 08:00:00"
// 方法3:轉(zhuǎn)成ISO標(biāo)準(zhǔn)格式(UTC時(shí)間)
const isoString = date.toISOString(); // "2023-10-01T00:00:00.000Z"4. 時(shí)間字符串 → 時(shí)間戳
將日期字符串解析為時(shí)間戳:
// 方法1:Date.parse()(需符合標(biāo)準(zhǔn)格式)
const timestamp4 = Date.parse('2023-10-01T00:00:00Z'); // 1696147200000(UTC時(shí)間)
// 方法2:new Date().getTime()
const dateStr = '2023-10-01 08:00:00'; // 假設(shè)本地時(shí)區(qū)為UTC+8
const timestamp5 = new Date(dateStr).getTime(); // 1696147200000(需注意時(shí)區(qū)問(wèn)題)
// 注意:非標(biāo)準(zhǔn)格式可能導(dǎo)致解析失敗,建議使用ISO格式(YYYY-MM-DDTHH:mm:ssZ)5. 常見(jiàn)場(chǎng)景示例
場(chǎng)景1:計(jì)算時(shí)間差
const start = Date.now(); // ...執(zhí)行某些操作 const end = Date.now(); const duration = end - start; // 毫秒數(shù)
場(chǎng)景2:倒計(jì)時(shí)功能
function formatCountdown(timestamp) {
const now = Date.now();
const diff = timestamp - now;
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
return `${days}天 ${hours}小時(shí)`;
}場(chǎng)景3:UTC與本地時(shí)間互轉(zhuǎn)
// UTC時(shí)間 → 本地時(shí)間
const utcDate = new Date('2023-10-01T00:00:00Z');
const localHours = utcDate.getHours(); // 本地時(shí)區(qū)的小時(shí)數(shù)(如UTC+8得到8)
// 本地時(shí)間 → UTC時(shí)間戳
const localDate = new Date(2023, 9, 1, 8, 0, 0); // 2023-10-01 08:00:00(本地時(shí)間)
const utcTimestamp = Date.UTC(2023, 9, 1, 0, 0, 0); // 1696147200000注意事項(xiàng)
時(shí)區(qū)問(wèn)題
new Date()和Date.parse()默認(rèn)使用本地時(shí)區(qū),而toISOString()和Date.UTC()使用 UTC 時(shí)區(qū)。- 跨時(shí)區(qū)應(yīng)用建議統(tǒng)一使用 UTC 時(shí)間。
時(shí)間戳單位
- JavaScript 使用 毫秒,與其他語(yǔ)言(如 Python 的秒)轉(zhuǎn)換時(shí)需注意單位換算:
const seconds = Math.floor(timestamp / 1000); // 毫秒轉(zhuǎn)秒 const milliseconds = seconds * 1000; // 秒轉(zhuǎn)毫秒
瀏覽器兼容性
- 避免使用非標(biāo)準(zhǔn)日期格式(如
'2023-10-01'不帶時(shí)間部分),不同瀏覽器解析結(jié)果可能不一致。
到此這篇關(guān)于JavaScript時(shí)間戳與時(shí)間的轉(zhuǎn)化常用方法的文章就介紹到這了,更多相關(guān)js時(shí)間戳與時(shí)間的轉(zhuǎn)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JavaScript將時(shí)間戳轉(zhuǎn)換為日期格式的多種轉(zhuǎn)換方法
- 利用JavaScript實(shí)現(xiàn)時(shí)間戳功能的5種方法詳解
- JavaScript?中的時(shí)間戳操作和使用詳解
- JavaScript時(shí)間戳與時(shí)間相互轉(zhuǎn)換的常用方法
- JavaScript獲取和操作時(shí)間戳的用法詳解
- javascript日期字符串轉(zhuǎn)換為時(shí)間戳的5種方法總結(jié)
- JS時(shí)間戳與日期格式的轉(zhuǎn)換小結(jié)
- 基于JavaScript編寫一個(gè)時(shí)間戳轉(zhuǎn)換工具
相關(guān)文章
layui自定義驗(yàn)證,用ajax查詢后臺(tái)是否有重復(fù)數(shù)據(jù),form.verify的例子
今天小編就為大家分享一篇layui自定義驗(yàn)證,用ajax查詢后臺(tái)是否有重復(fù)數(shù)據(jù),form.verify的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
簡(jiǎn)單實(shí)現(xiàn)js倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了js倒計(jì)時(shí)效果的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
In Javascript Class, how to call the prototype method.(three
In Javascript Class, how to call the prototype method.(three method)...2007-01-01
微信小程序簡(jiǎn)單實(shí)現(xiàn)form表單獲取輸入數(shù)據(jù)功能示例
這篇文章主要介紹了微信小程序簡(jiǎn)單實(shí)現(xiàn)form表單獲取輸入數(shù)據(jù)功能,涉及微信小程序針對(duì)form表單的事件綁定及數(shù)據(jù)獲取等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
openlayers4.6.5實(shí)現(xiàn)距離量測(cè)和面積量測(cè)
這篇文章主要為大家詳細(xì)介紹了openlayers4.6.5實(shí)現(xiàn)距離量測(cè)和面積量測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09

