question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dayjs does not preserve timezone information when serializing to JSON string

See original GitHub issue

When I do this

const timestamp = dayjs("2019-11-26T17:28:35.518+03:00")
const actualJson = JSON.stringify(timestamp)
const expectedJson = "\"2019-11-26T17:28:35.518+03:00\""

I want actualJson to be equal to expectedJson. However, actualJson is "\"2019-11-26T14:28:35.518Z\"". With Moment.js I can do this:

moment.fn.toJSON = function() { return this.format(); }

in order to get my expected JSON string with the preserved timezone information, see https://momentjs.com/docs/#/displaying/as-json/

How can I achieve this in dayjs? I am new to Javascript/Typescript and my compiler tells me that dayjs.fn does not exist. What would be the right workaround?

Information

  • Day.js Version 1.8.28
  • OS: Windows 8.1
  • Time zone: CEST (Berlin)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
svdHerocommented, Jun 24, 2020

Hm, there seems to be a more fundamental problem. Apparently, already the parsing does not preserve the timezone information. I have the following Jest test code:

describe("dajys", () => {
    it("preserves timezone information on roundtrip", () =>{
        const originalISOString = "2019-11-26T18:28:35.518+04:00"
        const ISO_FORMAT = "YYYY-MM-DDTHH:mm:ss.SSSZ"
        // ###############
        const actualString = dayjs(originalISOString).format(ISO_FORMAT)
        // ###############
        expect(actualString).toEqual(originalISOString)
    })
})

When I run this, I get a failed test with output:

    Expected: "2019-11-26T18:28:35.518+04:00"
    Received: "2019-11-26T15:28:35.518+01:00"

The date strings are equivalent, but the parsing obviously returned a moment in my local timezone (GMT+01, Berlin). This is exactly how moment.js behaves. However, moment.js has moment.parseZone(...), see https://momentjs.com/docs/#/parsing/, which does exactly what I want and makes the above test code succeed.

Does day.js have anything similar?

2reactions
iamkuncommented, Jun 22, 2020

As JSON will serialize Dayjs object to an ISO 8601 string. https://day.js.org/docs/en/display/as-json

However, if you want to customize this function, you can write your own plugin to modify it. https://day.js.org/docs/en/plugin/plugin

const JsonPlugin = (option, dayjsClass, dayjsFactory) => {
  // overriding existing API
  dayjsClass.prototype.toJSON = function() {
      return this.format()
   }
}
dayjs.extend(JsonPlugin)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dayjs does not preserve timezone information ... - Issuehunt
Dayjs does not preserve timezone information when serializing to JSON string #934 · Day.js Version 1.8.28 · OS: Windows 8.1 · Time zone:...
Read more >
How to JSON stringify a javascript Date and preserve timezone
If you have a JS Date Object and want to stringify it to preserve the timezone, then you should definitely use toLocaleDateString() ....
Read more >
Working with Dates and Times with Day.js
Day.js is a straightforward JavaScript tool for manipulating, displaying, and validating dates and times in modern browsers.
Read more >
Time Zone
Day.js supports time zone via the Internationalization API in supported environments. By using the native API, no extra bytes of timezone data need...
Read more >
Docs - Moment.js
We will release data updates for Moment-Timezone following IANA time zone database releases. Reasons to keep using Moment. In most cases, you should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found