您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。