简体   繁体   English

如何将日期转换成给定格式?

[英]How to convert date into given format?

I want to generate this date format using Moment.js: 我想使用Moment.js生成此日期格式:

Mon Apr 1 17:51:40 2019

Right now, I am getting this format instead: 现在,我得到的是这种格式:

Mon Apr 01 2019 17:51:40 GMT+0530 (IST)

use this: 用这个:

Moment(new Date(this.state.date)).format('MMMM Do YYYY, h:mm:ss a')

or whatever format you want. 或您想要的任何格式。 you can check them on here: https://momentjs.com/ 您可以在这里检查它们: https : //momentjs.com/

Try this: 尝试这个:

 var dateTime = new Date("Mon Apr 01 2019 17:51:40 GMT+0530 (IST)");
 dateTime = moment(dateTime).format("ddd MMM D HH:mm:ss YYYY");

已经是有效的日期字符串-只需输入一个new Date

 console.log(new Date("Mon Apr 01 2019 17:51:40 GMT+0530 (IST)")); 

first you need to install moment 首先你需要安装一下

 let formatedDate = moment('Mon Apr 01 2019 17:51:40 GMT+0530 (IST)').format('ddd MMM DD HH:mm:ss YYYY');
 console.log(formatedDate)

output: Mon Apr 01 17:51:40 2019 输出:2019年4月1日星期一17:51:40

Done You can check it 完成您可以检查一下

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

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