简体   繁体   中英

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

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).

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

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

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