欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript時(shí)間戳與時(shí)間的轉(zhuǎn)化常用方法

 更新時(shí)間:2025年05月08日 11:20:18   作者:風(fēng)格654  
在 JavaScript 中,時(shí)間戳(Timestamp)通常指Unix時(shí)間戳,即從1970年1月1日 00:00:00 UTC到某個(gè)時(shí)間點(diǎn)經(jīng)過的毫秒數(shù),下面給大家介紹JavaScript時(shí)間戳與時(shí)間的轉(zhuǎn)化常用方法,感興趣的朋友一起看看吧

在 JavaScript 中,時(shí)間戳(Timestamp)通常指 Unix 時(shí)間戳,即從 1970年1月1日 00:00:00 UTC 到某個(gè)時(shí)間點(diǎn)經(jīng)過的 毫秒數(shù)(注意:其他語言如 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í)間對象

用時(shí)間戳生成 Date 對象后,可提取具體時(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開始,需+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 對象格式化為易讀的字符串:

// 方法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ū)問題)
// 注意:非標(biāo)準(zhǔn)格式可能導(dǎo)致解析失敗,建議使用ISO格式(YYYY-MM-DDTHH:mm:ssZ)

5. 常見場景示例

場景1:計(jì)算時(shí)間差

const start = Date.now();
// ...執(zhí)行某些操作
const end = Date.now();
const duration = end - start; // 毫秒數(shù)

場景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í)`;
}

場景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ū)問題

  • new Date() 和 Date.parse() 默認(rèn)使用本地時(shí)區(qū),而 toISOString() 和 Date.UTC() 使用 UTC 時(shí)區(qū)。
  • 跨時(shí)區(qū)應(yīng)用建議統(tǒng)一使用 UTC 時(shí)間。

時(shí)間戳單位

  • JavaScript 使用 毫秒,與其他語言(如 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論