简体   繁体   中英

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.

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.

Guidance much appreciated.

use

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

considering you want to convert it to Date()

You were close the the right solution. You have to use the moment constructor with 2 parameters. The 1st parameter is your string date and the 2nd is the format.

In your case the correct format is 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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