简体   繁体   English

Node.js 将日期字符串转换为 unix 时间戳

[英]Node.js converting date string to unix timestamp

I'm attempting to convert a Date String to a unix timestamp in Node.js.我正在尝试将日期字符串转换为 Node.js 中的 unix 时间戳。

My code below works perfectly on my client but when I run it on my server I get an error:我下面的代码在我的客户端上运行良好,但是当我在我的服务器上运行它时,我收到一个错误:

(node:19260) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: input.substring is not a function (node:19260) UnhandledPromiseRejectionWarning: Unhandled promise Rejection (rejection id: 1): TypeError: input.substring is not a function

My code:我的代码:

function dateParser(input) {
    // function is passed a date and parses it to create a unix timestamp

    // removing the '.000' from input
    let finalDate = input.substring(0, input.length - 4);
    return new Date(finalDate.split(' ').join('T')).getTime();
}

Example of my input would be 2017-09-15 00:00:00.000我的输入示例是2017-09-15 00:00:00.000

So why does the above work on my client but not in Node, and how would I duplicate the functionality in node?那么为什么上述内容在我的客户端上有效,但在 Node 中无效,我将如何在 Node 中复制该功能?

Create a date object from your input DateTime string and then use getTime() and divide the result with 1000 to get the UNIX timestamp.从输入的 DateTime 字符串创建一个日期对象,然后使用getTime()并将结果除以 1000 以获得 UNIX 时间戳。

 var unixTimestamp = Math.floor(new Date("2017-09-15 00:00:00.000").getTime()/1000); console.log(unixTimestamp);

I will recommend using momentjs to handle the dates.我会推荐使用momentjs来处理日期。 Using momentjs you could do:使用momentjs你可以这样做:

moment().unix(); // Gives UNIX timestamp

If you already have a date and want to get the UNIX timestamp relative to that date, you could do:如果您已经有一个日期并希望获得相对于该日期的 UNIX 时间戳,您可以执行以下操作:

moment("2017-09-15 00:00:00.000").unix(); // I have passed the date that will be your input 
// Gives out 1505413800

It gets very productive when handling date/time using momentjs.使用 momentjs 处理日期/时间时会变得非常高效。

A Unix timestamp is the number of seconds from a specific date. Unix 时间戳是距特定日期的秒数。 The Javascript function getTime() returns the number of milliseconds from the same specific date til the date you specified. Javascript 函数 getTime() 返回从同一特定日期到您指定的日期之间的毫秒数。

So, if you divide the result of your function by number 1000 you will get the Unix timestamp and convert from milliseconds to seconds.因此,如果您将函数的结果除以数字 1000,您将获得 Unix 时间戳并将毫秒转换为秒。 Don't forget to ignore decimal places.不要忘记忽略小数位。

The error message you are receiving is because the value of input is not a string.您收到的错误消息是因为 input 的值不是字符串。

2 options with slightly different results if system timezone is not set to UTC如果系统时区未设置为 UTC,则 2 个选项的结果略有不同

Option 1 - Ignores system timezone选项 1 - 忽略系统时区

console.log(Math.floor(new Date("2020-01-01").getTime()/1000));

> 1577836800

Option 2 ("moment.js") - Timestamp will differ with system timezone选项 2 ("moment.js") - 时间戳会因系统时区而异

console.log(moment('2020-01-01').unix());

> 1577854800

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM