1

I am having a date in the format 2022-01-27 09:23:48 UTC and I am parsing the date into MMMM-DD-YYYY format (Jan-27-2022) using day.js .Its working fine in chrome but firefox is returning Invalid.

import dayjs from "dayjs"

const utcDate = '2022-01-27 09:23:48 UTC'
const formatedDate = dayjs(utcDate).format("MMMM-DD-YYYY")

I have also tried the customParseFormat mentioned in the docs and some other threads

import dayjs from "dayjs"
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(customParseFormat)

const utcDate = '2022-01-27 09:23:48 UTC'
const formatedDate = dayjs(utcDate, 'MMMM-DD-YYYY')

In both cases its returning 'Invalid Date' in firefox and safari but working fine in chrome. Is there any work around for this?

Manu J
  • 133
  • 9
  • 2
    I can reproduce but it is fixed by providing a valid ISO8601 date string (or providing a format pattern to `dayjs` (you have to take off the UTC) `dayjs(utcDate.slice(0, -4), 'YYYY-MM-DD HH:mm:ss').format("MMMM-DD-YYYY")` – pilchard Feb 04 '22 at 15:47
  • Your input string doesn't match the ISO8601 format, and none of the [custom parse tokens](https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens) would match `UTC` - you can only specify the offset from UTC, not a time-zone code. I suspect you'll need to strip off the `UTC` suffix before parsing. – Richard Deeming Feb 04 '22 at 15:49
  • `MMMM-DD-YYYY` in `dayjs(utcDate, 'MMMM-DD-YYYY')` is the format string [`dayjs`](https://day.js.org/docs/en/parse/string-format) will use to parse the string in `utcDate` which obviously doesn't match the given string. – Andreas Feb 04 '22 at 15:51
  • @pilchard `dayjs` doesn't care about the end of the string when using a format string for parsing, hence the `.slice(0, -4)` is not necessary. – Andreas Feb 04 '22 at 15:53
  • 1
    @Andreas just tested in Firefox and it fails if the UTC remains, thus the `.slice(0, -4)` – pilchard Feb 04 '22 at 15:53
  • 1
    @pilchard Works like a charm for me and my FF91ESR: https://jsfiddle.net/05zweqgs/ – Andreas Feb 04 '22 at 15:57
  • Issue Resolved when slicing end String as mentioned by @pilchard, Thank You – Manu J Feb 04 '22 at 15:58
  • @Andreas true with the with the customParseFormat extension, but fails without. – pilchard Feb 04 '22 at 16:01

1 Answers1

0

Issue got resolved. Credits @pilchard

import dayjs from "dayjs"

const utcDate = '2022-01-27 09:23:48 UTC'
const formatedDate = dayjs(utcDate.slice(0, -4)).format("MMMM-DD-YYYY")
Manu J
  • 133
  • 9
  • Please see the [day.js documentation](https://day.js.org/docs/en/parse/string): "*For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.*". The OP format is not ISO 8601 compliant. Removing the " UTC" part means the string is parsed as local, so no longer UTC. If, by chance, the host system has offset +0 then it still represents the same date and time. However, for any other offset, it now represents a different moment in time. – RobG Feb 04 '22 at 21:49
  • Also see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Feb 04 '22 at 21:50
  • @RobG, so is it better first we convert utc to local time format and do the formatting using dayjs – Manu J Feb 05 '22 at 04:29
  • No. To preserve the original date and time, use the [*utc* method](https://day.js.org/docs/en/parse/utc): `dayjs.utc(utcDate.slice(0, -4))`. – RobG Feb 05 '22 at 09:19