简体   繁体   中英

momentJS convert possibility day into Date() js format

I am trying to convert a date in format momentjs into a date from javascript native new Date().

The problem is that if I have moment(myDay).toDate(); it converts to the current date, and I want the date from myDay.

myDay looks like: "YYYY-MM-DD" => 2017-11-24 and I would like to have it with the format: Fri Nov 24 2017 20:17:11 GMT+0100 (Hora estándar romance) but I get Thu Nov 16 2017 etc...

It is possible to convert it like that way?

Don't need moment:

let [yr, mn, day] = myDay.split('-').map(Number);
// note that JS months are 0-11 not 1-12
let datestr = new Date(yr, mn - 1, dy).toString();
console.log(datestr); // "Fri Nov 24 2017 00:00:00 GMT-0500 (EST)"

you want something like this:

moment(myDay, "YYYY-MM-DD").toString();

moment().toString() Returns an english string in a similar format to JS Date's .toString().

moment().toString() // "Sat Apr 30 2016 16:59:46 GMT-0500"

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