Exception
Exception

Reputation: 8379

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

Upvotes: 1

Views: 15145

Answers (2)

Victor Ventura
Victor Ventura

Reputation: 1

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

Upvotes: 0

hsz
hsz

Reputation: 152216

Just try with:

'YYYY-MM-DDThh:mm:ssZ'

Output:

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

moment.js format documentation

Upvotes: 11

Related Questions