简体   繁体   中英

Javascript - Create new Date using a UK format and moment.js

I have a date in UK format "DD/MM/YYYY HH:mm" and I want to create a new date with javascript. I'm trying the following using moment.js but it doesn't recognise the timepart and simply adds on the current time.

 var myDate = "11/10/2016 09:00"

 // The toDate() is equivalent to the javascript new Date() function
 var newDate = moment(myDate, "YYYY-MM-DD HH:mm").val()).toDate();

 // output:
 // 20161011T091406Z 
 // Tue Oct 11 2016 09:14:06 GMT+0100

As you can see its changed the time part to the time the script is run.

Figured it out myself by doing the following:

var myDate = "11/10/2016 09:00"

myDate  = moment(myDate, "DD/MM/YYYY HH:mm").toISOString();

var newDate = moment(myDate).toDate(); 

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