# JavaScript時間戳怎么轉化為日期
## 什么是時間戳
時間戳(Timestamp)是指從格林威治時間1970年1月1日00:00:00(UTC)起至現在的總秒數或毫秒數。在JavaScript中,時間戳通常以毫秒為單位。
```javascript
// 獲取當前時間的時間戳(毫秒)
const timestamp = Date.now();
console.log(timestamp); // 例如:1714567890123
const timestamp = 1714567890123;
const date = new Date(timestamp);
console.log(date);
// 輸出:Wed May 01 2024 12:51:30 GMT+0800 (中國標準時間)
const date = new Date(1714567890123);
const year = date.getFullYear(); // 2024
const month = date.getMonth() + 1; // 5(注意月份從0開始)
const day = date.getDate(); // 1
const hours = date.getHours(); // 12
const minutes = date.getMinutes(); // 51
const seconds = date.getSeconds(); // 30
function formatDate(timestamp) {
const date = new Date(timestamp);
return `${date.getFullYear()}-${padZero(date.getMonth()+1)}-${padZero(date.getDate())} ${padZero(date.getHours())}:${padZero(date.getMinutes())}:${padZero(date.getSeconds())}`;
}
function padZero(num) {
return num < 10 ? `0${num}` : num;
}
console.log(formatDate(1714567890123));
// 輸出:2024-05-01 12:51:30
const date = new Date(1714567890123);
// 本地化日期時間
console.log(date.toLocaleString());
// 輸出:2024/5/1 12:51:30(中文環境)
// 指定格式
console.log(date.toLocaleString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
}));
// 輸出:May 1, 2024
const moment = require('moment');
console.log(moment(1714567890123).format('YYYY-MM-DD HH:mm:ss'));
// 輸出:2024-05-01 12:51:30
const { format } = require('date-fns');
console.log(format(new Date(1714567890123), 'yyyy-MM-dd HH:mm:ss'));
// 輸出:2024-05-01 12:51:30
function convertTimezone(timestamp, timezone) {
const date = new Date(timestamp);
return date.toLocaleString('en-US', {
timeZone: timezone,
hour12: false
});
}
console.log(convertTimezone(1714567890123, 'America/New_York'));
// 輸出:5/1/2024, 00:51:30(紐約時間)
// 如果時間戳是秒級(10位),需要乘以1000
const secondsTimestamp = 1714567890;
const date = new Date(secondsTimestamp * 1000);
// Safari對某些日期格式支持不好,建議使用:
const date = new Date(parseInt(timestamp));
function dateDiff(timestamp1, timestamp2) {
const diff = Math.abs(timestamp1 - timestamp2);
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
return days;
}
// 高效格式化示例
const formatter = new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
function formatMultiple(timestamps) {
return timestamps.map(ts => formatter.format(new Date(ts)));
}
function formatLogTime(timestamp) {
const now = Date.now();
const diff = now - timestamp;
if (diff < 60000) return '剛剛';
if (diff < 3600000) return `${Math.floor(diff/60000)}分鐘前`;
const date = new Date(timestamp);
return `${date.getHours()}:${date.getMinutes()}`;
}
function generateWeekDays(timestamp) {
const date = new Date(timestamp);
const day = date.getDay();
const start = timestamp - day * 86400000;
return Array(7).fill(0).map((_, i) => {
return new Date(start + i * 86400000);
});
}
JavaScript中時間戳轉換主要通過Date對象實現,關鍵點包括:
選擇合適的方法取決于具體需求,簡單場景用原生API,復雜日期處理推薦date-fns等現代庫。
”`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。