简体   繁体   English

将日期转换为指定格式 moment.js

[英]Convert a Date to specified format moment.js

I have a date stored in variable like below我有一个日期存储在变量中,如下所示

var date = moment(new Date()).valueOf();

I need to format it like below我需要像下面这样格式化

CCYY-MM-DDThh:mm:ss[.sss]TZD CCYY-MM-DDThh:mm:ss[.sss]TZD

The Time Zone Definition is mandatory and MUST be either UTC (denoted by addition of the character 'Z' to the end of the string) or some offset from UTC (denoted by addition of '[+|-]' and 'hh:mm' to the end of the string).时区定义是强制性的,并且必须是 UTC(通过在字符串末尾添加字符 'Z' 表示)或与 UTC 的某个偏移量(通过添加 '[+|-]' 和 'hh:mm ' 到字符串的末尾)。

I have tried like below我试过如下

var required = moment.utc(date).format('CCYY-MM-DDThh:mm:ss[.sss]TZD')

But it is resulting like below但结果如下

"CC14-06-03T07:59:15.sssT+00:003"

But the expected format examples are但预期的格式示例是

UTC : 

1969-07-21T02:56:15Z

Houston time :

1969-07-20T21:56:15-05:00

Just try with:只需尝试:

'YYYY-MM-DDThh:mm:ssZ'

Output:输出:

2014-06-03T08:16:15+00:00 

moment.js format documentation moment.js 格式文档

Code:代码:

new Date(moment('14/07/2022 14:27:50', 'DD/MM/YYYY HH:mm:ss').format('YYYY-MM-DDTHH:mm:ss'))

Output:输出:

2022-07-14T17:27:50.000Z

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

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