
纪元时间 (Epoch Time),也称为 Unix 时间或 POSIX(Portable Operating System Interface) 时间,是一种将时间戳表明为单个整数值的系统。用于计算自特定参考点(称为 “纪元”)以来经过的秒数,此参考点一般定义为 1970 年 1 月 1 日 00:00:00 的世界协调时间 (Coordinated Universal Time,即 UTC)。
从这点看,Unix 时间和 POSIX 时间的计算和 UTC 时间有关。

纪元时间一般用于各种 JavaScript 应用程序,例如:时间戳、测量时间间隔和执行与时间相关的计算等等。本文将带着大家一起了解如何使用纪元时间计算当地时间,并使用 JavaScript 将其转换为 UTC 时间。
开始之前,先就纪元时间 (Epoch Time)、Unix 时间或 POSIX 时间做下简单说明:
- 纪元时间: 用来描述时间系统的概念,不限于某个特定操作系统。
- Unix 时间: 强调其在 Unix 系统中的起源和使用
- POSIX 时间: 强调其标准化的接口和广泛的跨平台适用性
1. 如何从纪元或 UNIX 时间获取当地时间
JavaScript Date 对象以与平台无关的格式表明某个时刻,Date 对象封装了一个整数,表明自 1970 年 1 月 1 日午夜(UTC,纪元)以来的毫秒数。因此,开发者可以使用 Date.now() 获取纪元时间,即 Unix 时间戳,列如下面的示例:
// 以毫秒单位返回当前纪元时间
const currentEpochTime = Date.now();
console.log(currentEpochTime);
// 输出 1739236770398ms
const currentEpochTimeInSeconds = Math.floor(Date.now() / 1000);
console.log(currentEpochTimeInSeconds);
// 转化为秒
下面示例从纪元或 UNIX 时间戳中提取当地日期和时间:
// 以 ms 为单位获取当前纪元时间
const currentEpochTime = Date.now();
// 根据当地日期和时间(以毫秒为单位)创建 Date 对象
var localDate = new Date(currentLocalDateMillis);
// 将当地日期和时间格式化为字符串(使用本地时区)
var localDateTimeString = localDate.toLocaleString();
// 使用默认配置
console.log("Local Date and Time:", localDateTimeString);
// 输出 8/17/2024, 8:52:55 PM
如果开发者只想提取日期或时间,可以使用以下日期方法:
- getFullYear(): 获取四位数字形式的年份 (yyyy)
- getMonth(): 获取数字形式的月份 (0-11)
- getDate() :获取数字形式的日期 (1-31)
- getDay() :获取数字形式的星期几 (0-6)
- getHours() :获取小时 (0-23)
- getMinutes() :获取分钟 (0-59)
- getSeconds() :获取秒 (0-59)
- getMilliseconds(): 获取毫秒 (0-999)
- getTime() :获取时间(自 1970 年 1 月 1 日以来的毫秒数)
开发者可以使用上述方法从纪元或 UNIX 时间中提取当前时间:
function convertEpochToLocalTime(epochTimestamp) {
const date = new Date(epochTimestamp * 1000);
// 转化为 ms
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
let formattedHours = hours % 12;
if (formattedHours === 0) {
formattedHours = 12;
// 12:00 AM or 12:00 PM
}
const amOrPm = hours < 12 ? "AM" : "PM";
const formattedTime = `${formattedHours}:${minutes
.toString()
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")} ${amOrPm}`;
return formattedTime;
}
const epochTimestamp = 1739236770;
const formattedTime = convertEpochToLocalTime(epochTimestamp);
console.log(formattedTime);
// 输出 9:19:30 AM
2. 如何从纪元或 UNIX 时间中提取 UTC 时间
要在 JavaScript 中将纪元时间戳转换为 UTC 时间,开发者依然可使用 Date 对象。
const epochTimestamp = 1739236770;
// 将纪元时间转化为 UTC 日期和时间
const utcDate = new Date(epochTimestamp * 1000);
console.log("UTC Time:", utcDate.toUTCString());
// 输出”UTC Time: Tue, 11 Feb 2025 01:19:30 GMT“
GMT 表明 Greenwich Mean Time,是不思考时区偏移的(零时区本初子午线),即标准的 UTC 时间
1739236770 代表特定时刻的纪元时间戳,Date.toUTCString() 方法用于获取 UTC 日期和时间的字符串表明形式。如果想提取日期或时间,可以使用以下方法:
- getUTCDate(): 返回 UTC 日期
- getUTCFullYear(): 返回 UTC 年份
- getUTCMonth(): 返回 UTC 月份
- getUTCDay() :返回 UTC 天
- getUTCHours() :返回 UTC 小时
- getUTCMinutes() :返回 UTC 分钟
- getUTCSeconds() :返回 UTC 秒
- getUTCMilliseconds(): 返回 UTC 毫秒
开发者可以使用上述方法从纪元或 UNIX 时间中提取当前时间:
const epochTimestamp = 1739236770;
// 将纪元时间转化为 UTC 日期和时间
const utcDate = new Date(epochTimestamp * 1000);
// 单独获取 UTC 时间组成
const hours = utcDate.getUTCHours();
const minutes = utcDate.getUTCMinutes();
const seconds = utcDate.getUTCSeconds();
const twelveHourFormat = hours % 12 || 12;
const ampm = hours < 12 ? "AM" : "PM";
// 构造指定格式的时间
const formattedTime = `${twelveHourFormat}:${minutes}:${seconds} ${ampm}`;
console.log("UTC Time (12-hour format):", formattedTime);
// 输出结果 ”UTC Time (12-hour format): 1:19:30 AM“
3. 本文结论
纪元时间是时间的一种数字表明,其计算自预定义起点(Unix 纪元,即 1970 年 1 月 1 日)以来经过的秒数或毫秒数。其为计算机提供了一种标准化的方法来管理时间,便于跨不同系统和位置进行比较和计算。
参考资料
https://medium.com/@kushal.bhargava01/what-is-epoch-time-also-know-as-unix-time-or-posix-time-bd8efd1a491
https://www.unixtutorial.org/unix-epoch/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
https://techterms.com/definition/epoch_time

















暂无评论内容