简体   繁体   中英

Date Formatting is not working in momentjs?

Following date formatting is not working in momentJs

 var date ="01-12-2015";//DD-MM-YYY
 console.info(moment(date).format('YYYY-MM-DD'));

How can I solve this?

You need to tell momentJS how to parse the string you are giving:

var date ="01-12-2015";//DD-MM-YYYY
console.info(moment(date, 'DD-MM-YYYY').format('YYYY-MM-DD'));

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

Try this..

var date ="01-12-2015";//DD-MM-YYY
date = date.replace(/-/g,"/")
console.info(moment(new Date(date)).format('YYYY-MM-DD'));

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