Dayjs does not preserve timezone information when serializing to JSON string
See original GitHub issueWhen 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:
- Created 3 years ago
- Reactions:3
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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:
When I run this, I get a failed test with output:
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?
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