简体   繁体   English

RFC2822 日期的 Moment.js 转换

[英]Moment.js conversion of RFC2822 date

I have a date as '04 FEB 1994' which I'm trying to convert into a date object for comparison.我有一个日期为“1994 年 2 月 4 日”,我试图将其转换为日期 object 以进行比较。

I've tried a whole bunch of variations, the current one is...我已经尝试了一大堆变体,目前的一个是......

var trydate = moment(licDob, "DD-MMM-YYYY");
var momentObj = moment(trydate);
var momentString = momentObj.format('YYYY-MM-DD');

where licDob is 04 FEB 1994. momentObj is coming back as 853506000000 which makes momentString 1997-01-18.其中 licDob 是 1994 年 2 月 4 日。momentObj 以 853506000000 的形式返回,这使得momentString 为 1997-01-18。

Guidance much appreciated.非常感谢指导。

use利用

var newTry = moment(licDob,'DD MMM YYYY').toDate()

considering you want to convert it to Date()考虑到您想将其转换为 Date()

You were close the the right solution.你接近正确的解决方案。 You have to use the moment constructor with 2 parameters.您必须使用带有 2 个参数的moment构造函数。 The 1st parameter is your string date and the 2nd is the format.第一个参数是您的字符串日期,第二个是格式。

In your case the correct format is DD MMM YYYY在您的情况下,正确的格式是DD MMM YYYY

 const stringDate = "04 FEB 1994"; const momentDate = moment(stringDate, "DD MMM YYYY"); console.log("Formatted:", momentDate.format("DD/MM/YYYY")); console.log("EPOCH:", momentDate.valueOf())
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

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

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