使用 JavaScript 将日期转换为时间戳
Convert a Date to a Timestamp using JavaScript
使用该getTime()
方法将日期转换为时间戳,例如
new Date().getTime()
. 该getTime
方法返回从 1970 年 1 月 1 日到给定日期之间经过的毫秒数。
const str = '2022-04-26'; const date = new Date(str); // ✅ Get timestamp in Milliseconds const timestamp = date.getTime(); console.log(timestamp); // 👉️ 1650931200000 // ✅ If you need to convert to Seconds const timestampSeconds = Math.floor(date.getTime() / 1000); console.log(timestampSeconds); // 👉️ 1650931200
要将 a 转换Date
为时间戳,您需要有一个Date
对象。
如果您有日期字符串,请将其传递给
Date()
构造函数以获取Date
对象。
Date
,则需要在将字符串传递给构造函数之前正确格式化字符串(更多内容见下文)。 Date()
getTime
方法返回自 Unix 纪元(1970 年 1 月 1 日 00:00:00)以来的毫秒数。
如果您需要将结果转换为秒,请将其除以1000
。
const str = '2022-04-26'; const date = new Date(str); // ✅ If you need to convert to Seconds const timestampSeconds = Math.floor(date.getTime() / 1000); console.log(timestampSeconds); // 👉️ 1650931200
简而言之,要将 a 转换Date
为时间戳,您只需
getTime()
调用Date
.
如果您在创建有效Date
对象时遇到困难,可以将 2 种类型的参数传递给Date()
构造函数:
- 一个有效的 ISO 8601 字符串,格式为
YYYY-MM-DDTHH:mm:ss.sssZ
, 或者只是
YYYY-MM-DD
,如果你只有一个没有时间的日期。 - 多个逗号分隔的参数,表示
year
,month
(0 = 一月到 11 = 十二月)
day of the month
、、、hours
和。minutes
seconds
Here is an example that splits a string and passes the parameters to the
Date()
constructor to create a Date
object.
// 👇️ Formatted as MM/DD/YYYY const str = '04/16/2022'; const [month, day, year] = str.split('/'); const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Sat Apr 16 2022 // ✅ Get timestamp const timestamp = date.getTime(); console.log(timestamp); // 👉️ 1650056400000
MM/DD/YYYY
, so we split the string on each forward slash to get an array of substring containing the month, day and year.We passed the values as the first 3 parameters to the Date()
constructor to
create a valid Date
object, so we can get a timestamp by calling the
getTime()
method.
Notice that we subtracted 1
from the month when passing it to the Date()
constructor.
Date
constructor expects a zero-based value, where January = 0, February = 1, March = 2, etc.Here is another example, which creates a Date
that also contains the hours,
minutes and seconds and gets its timestamp.
// 👇️ Formatted as MM/DD/YYYY hh:mm:ss const str = '04/16/2022 06:45:12'; const [dateComponents, timeComponents] = str.split(' '); console.log(dateComponents); // 👉️ "04/16/2022" console.log(timeComponents); // 👉️ "06:45:12" const [month, day, year] = dateComponents.split('/'); const [hours, minutes, seconds] = timeComponents.split(':'); const date = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); console.log(date); // 👉️ Sat Apr 16 2022 06:45:12 // ✅ Get timestamp const timestamp = date.getTime(); console.log(timestamp); // 👉️ 1650080712000
The first thing we did was split the date and time string on the space, so we
can get the date and time components as separate strings.
We also split the time string on each colon and assigned the hours, minutes and
seconds to variables.
We passed all of the parameters to the Date()
constructor to create a Date
object and got the timestamp by calling the getTime()
method.