简体   繁体   English

date-fns 解析“/Date(1633421520000)/”

[英]date-fns parse for "/Date(1633421520000)/"

receiving string of date that looks like this "/Date(1633421520000)/" ,接收看起来像这样的日期字符串"/Date(1633421520000)/"

with moment I could just use it as moment("/Date(1633421520000)/") whats the equivalent for date-fns ?有了 moment,我可以将它用作moment("/Date(1633421520000)/") date- moment("/Date(1633421520000)/")的等价物是什么

for example differenceInMilliseconds how would i use it with received argument as this string "/Date(1633421520000)/"例如differenceInMilliseconds我将如何将它与接收到的参数一起用作此字符串"/Date(1633421520000)/"

not sure how to create my date object from this string so ill be able to use date-fns functions.不知道如何从这个字符串创建我的日期对象,所以我不能使用 date-fns 函数。

You need to extract the number (which looks like milliseconds since unix epoch) from the string:您需要从字符串中提取数字(它看起来像 unix 纪元以来的毫秒数):

"/Date(1634717139973)/".match(/\d+/)[0]

Then use Date constructor like so:然后像这样使用Date构造函数:

var date = new Date(Number("/Date(1634717139973)/".match(/\d+/)[0]));
date.toISOString(); // 2021-10-20T08:05:39.973Z

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

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