Moment.Js - Utc Gives Wrong Date

Moment.js returning wrong date

If you want the time in utc, use:

moment.utc('2013-10-29T00:00:00.000')

As @MattJohnson pointed out, using the moment constructor will translate it to local time. Instead (if you don't want to use the utc method), you can replace the Z with +0. See options for string date/time http://momentjs.com/docs/#/parsing/string-format/

moment.js - UTC gives wrong date

By default, MomentJS parses in local time. If only a date string (with no time) is provided, the time defaults to midnight.

In your code, you create a local date and then convert it to the UTC timezone (in fact, it makes the moment instance switch to UTC mode), so when it is formatted, it is shifted (depending on your local time) forward or backwards.

If the local timezone is UTC+N (N being a positive number), and you parse a date-only string, you will get the previous date.

Here are some examples to illustrate it (my local time offset is UTC+3 during DST):

>>> moment('07-18-2013', 'MM-DD-YYYY').utc().format("YYYY-MM-DD HH:mm")
"2013-07-17 21:00"
>>> moment('07-18-2013 12:00', 'MM-DD-YYYY HH:mm').utc().format("YYYY-MM-DD HH:mm")
"2013-07-18 09:00"
>>> Date()
"Thu Jul 25 2013 14:28:45 GMT+0300 (Jerusalem Daylight Time)"

If you want the date-time string interpreted as UTC, you should be explicit about it:

>>> moment(new Date('07-18-2013 UTC')).utc().format("YYYY-MM-DD HH:mm")
"2013-07-18 00:00"

or, as Matt Johnson mentions in his answer, you can (and probably should) parse it as a UTC date in the first place using moment.utc() and include the format string as a second argument to prevent ambiguity.

>>> moment.utc('07-18-2013', 'MM-DD-YYYY').format("YYYY-MM-DD HH:mm")
"2013-07-18 00:00"

To go the other way around and convert a UTC date to a local date, you can use the local() method, as follows:

>>> moment.utc('07-18-2013', 'MM-DD-YYYY').local().format("YYYY-MM-DD HH:mm")
"2013-07-18 03:00"

moment.js returning wrong day for a date

Changed the line to the following:

moment(document.getElementById('date').value + " 12:00", "DD-MM-YYYY HH:mm:ss").utc().format("YYYY-MM-DD HH:mm")

Based of this answer: moment.js - UTC gives wrong date

moment timezone js returns wrong datetime

The problem you have is caused by the Date constructor parsing the time according to the local timezone (in Chrome) and UTC (on Safari?).

For the time given, this code

moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss')

returns

"2021-07-22T21:49:47"

You then pass this time into the Date constructor:

var currentDateTime = new Date("2021-07-22T21:49:47")

Because this string has no time zone information, it is assumed to be in the device's local timezone (Chrome) or UTC (Safari) which gives the wrong date object.

While you could inject the timezone into this string (using ZZ), you can build a Date object from the moment object using:

var currentDateTime = moment().tz('Asia/Tokyo').toDate()

but this is effectively the same as

var currentDateTime = new Date()

If the intended output is a string of the format:

"Thu Jul 22 2021 21:49:47 GMT+0900 (Japan Standard Time)"

the closest you could get is:

moment().tz('Asia/Tokyo').format('ddd MMM D YYYY, h:mm:ss a [GMT]ZZ (z)')
// Thu Jul 22 2021 21:49:47 GMT+0900 (JST)

Alternatively, there is also the localized "Month name, day of month, day of week, year, time" format:

moment().tz('Asia/Tokyo').format('llll (z)')
// In my locale, I get:
// Thu, Jul 22, 2021 9:49 PM (JST)

Take a look at the Moment display format documentation for more options.

Why is moment converting this date incorrectly?

There are two issues here:

1) Moment is performing timezone conversion using your local timezone - use moment.utc instead

2) Your date is not in a format that moment "officially" supports - although actually it's relaxed enough to parse your string. Ideally, it should be provided in proper ISO 8601 format to avoid any compatibility issues.

You could try something like:

 const dbdate = '2019-06-27 20:00:43.156257'.split(' ');
const momentdate = moment.utc(dbdate[0] + 'T' + dbdate[1] + 'Z');
alert(momentdate);

Here's a fiddle.

Hope this helps!

Moment JS is showing a wrong date

You should pass the timestamp as a number not a string.

Example:

moment(scrape_lastdate.$date).format("DD-MM-YYYY hh:mm:ss")

Check the function signature in the Moment.js docs

moment(Number)

Some additional information between the differences of those 2 functions from the docs.

moment(Number);

Similar to new Date(Number), you can create a moment by passing an integer value representing the number of milliseconds since the Unix Epoch (Jan 1 1970 12AM UTC).

moment.unix(Number)

To create a moment from a Unix timestamp (seconds since the Unix Epoch), use moment.unix(Number).

moment JS returns wrong date

Thanks to Ian, this ended up being the solution.

moment.unix(dateTime).tz(timezone).format(format);

I was trying moment.utc() instead of moment.unix().

The strange results came from moment.utc(datetime, format) expecting datetime to match format. However, it's important to note that moment.utc(datetime) still returns 1970 year result so it still wouldn't have returned the desired result even without the format.

Incorrect date conversion of JSON dates using moment, OffSet value is ignored

It should be:

moment.parseZone('/Date(1742130000000+1100)/').format('DD-MMM-YYYY')


Related Topics



Leave a reply



Submit