Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format timestamp according to RFC 3339 with moment.js

Tags:

momentjs

Like this:

const RFC_3339 = 'YYYY-MM-DDTHH:mm:ss';
moment.utc().format(RFC_3339);

I need the timestamp to have a 'Z' at the end. Is there a better way than just +'Z'?

It should match the python code on the backend:

RFC_3339_FMT = "%Y-%m-%dT%H:%M:%SZ"
like image 974
kev Avatar asked Nov 01 '16 06:11

kev


People also ask

How do you format time in a moment?

To get the current date and time, just call javascript moment() with no parameters like so: var now = moment(); This is essentially the same as calling moment(new Date()) . Unless you specify a time zone offset, parsing a string will create a date in the current time zone.

What does format () do in moment?

format(); moment(). format(String); This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.

What is _D and _I in moment?

_i will never be a moment object. _i can also be undefined, in the case of creating the current moment with moment() . _d is the instance of the Date object that backs the moment object. If you are in "local mode", then _d will have the same local date and time as the moment object exhibits with the public API.


1 Answers

You can simply use format().

As the docs says:

As of version 1.5.0, calling moment#format without a format will default to moment.defaultFormat. Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ.

As of version 2.13.0, when in UTC mode, the default format will return Z as the offset, instead of +00:00

like image 72
VincenzoC Avatar answered Oct 04 '22 11:10

VincenzoC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!