使用 JavaScript 将日期转换为时间戳

使用 JavaScript 将日期转换为时间戳

Convert a Date to a Timestamp using JavaScript

使用该getTime()方法将日期转换为时间戳,例如
new Date().getTime(). getTime方法返回从 1970 年 1 月 1 日到给定日期之间经过的毫秒数。

索引.js
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

索引.js
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()构造函数:

  1. 一个有效的 ISO 8601 字符串,格式为YYYY-MM-DDTHH:mm:ss.sssZ, 或者只是
    YYYY-MM-DD,如果你只有一个没有时间的日期。
  2. 多个逗号分隔的参数,表示year, month(0 = 一月到 11 = 十二月
    day of the month、、、hoursminutesseconds

Here is an example that splits a string and passes the parameters to the
Date() constructor to create a Date object.

index.js
// 👇️ 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
We have a date string that is formatted as 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.

This is because, the 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.

index.js
// 👇️ 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 then had to split the date string on each forward slash to get the value for the month, day and year. Note that your separator might be different, e.g. a hyphen, but the approach is the same.

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.