亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript時間戳與時間的轉化常用方法

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

在 JavaScript 中,時間戳(Timestamp)通常指 Unix 時間戳,即從 1970年1月1日 00:00:00 UTC 到某個時間點經(jīng)過的 毫秒數(shù)(注意:其他語言如 Python 可能使用秒,但 JavaScript 默認用毫秒)。以下是時間戳與時間格式相互轉換的常用方法:

1. 獲取當前時間戳

// 方法1:Date.now()
const timestamp1 = Date.now();
// 方法2:new Date().getTime()
const timestamp2 = new Date().getTime();
// 方法3:+new Date()
const timestamp3 = +new Date();

2. 時間戳 → 時間對象

用時間戳生成 Date 對象后,可提取具體時間信息:

const timestamp = 1696147200000; // 示例時間戳(2023-10-01 00:00:00 UTC)
const date = new Date(timestamp);
// 提取時間信息(本地時區(qū))
const year = date.getFullYear();     // 2023
const month = date.getMonth() + 1;   // 10(注意月份從0開始,需+1)
const day = date.getDate();          // 1
const hours = date.getHours();       // 8(假設時區(qū)為UTC+8)
const minutes = date.getMinutes();   // 0
const seconds = date.getSeconds();   // 0
// 提取UTC時間信息
const utcHours = date.getUTCHours(); // 0(UTC時間)

3. 時間戳 → 格式化字符串

將 Date 對象格式化為易讀的字符串:

// 方法1:使用內(nèi)置方法(本地時區(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:手動拼接(靈活定制)
const formattedTime = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours}:${minutes}:${seconds}`;
// "2023-10-01 08:00:00"
// 方法3:轉成ISO標準格式(UTC時間)
const isoString = date.toISOString(); // "2023-10-01T00:00:00.000Z"

4. 時間字符串 → 時間戳

將日期字符串解析為時間戳:

// 方法1:Date.parse()(需符合標準格式)
const timestamp4 = Date.parse('2023-10-01T00:00:00Z'); // 1696147200000(UTC時間)
// 方法2:new Date().getTime()
const dateStr = '2023-10-01 08:00:00'; // 假設本地時區(qū)為UTC+8
const timestamp5 = new Date(dateStr).getTime(); // 1696147200000(需注意時區(qū)問題)
// 注意:非標準格式可能導致解析失敗,建議使用ISO格式(YYYY-MM-DDTHH:mm:ssZ)

5. 常見場景示例

場景1:計算時間差

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

場景2:倒計時功能

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}小時`;
}

場景3:UTC與本地時間互轉

// UTC時間 → 本地時間
const utcDate = new Date('2023-10-01T00:00:00Z');
const localHours = utcDate.getHours(); // 本地時區(qū)的小時數(shù)(如UTC+8得到8)
// 本地時間 → UTC時間戳
const localDate = new Date(2023, 9, 1, 8, 0, 0); // 2023-10-01 08:00:00(本地時間)
const utcTimestamp = Date.UTC(2023, 9, 1, 0, 0, 0); // 1696147200000

注意事項

時區(qū)問題

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

時間戳單位

  • JavaScript 使用 毫秒,與其他語言(如 Python 的秒)轉換時需注意單位換算:
const seconds = Math.floor(timestamp / 1000); // 毫秒轉秒
const milliseconds = seconds * 1000;          // 秒轉毫秒

瀏覽器兼容性

  • 避免使用非標準日期格式(如 '2023-10-01' 不帶時間部分),不同瀏覽器解析結果可能不一致。

到此這篇關于JavaScript時間戳與時間的轉化常用方法的文章就介紹到這了,更多相關js時間戳與時間的轉化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論