dayjs
dayjs copied to clipboard
format() not corresponding to date.
I have the following ISO-formatted string:
2022-02-28T13:57:02.000Z
When I do console.log(dayjs(isoString).tz("Australia/Sydney")) the date object prints as follows:
M {
'$L': 'en',
'$d': 2022-02-28T23:57:02.000Z,
'$x': { '$timezone': 'Australia/Sydney' },
'$y': 2022,
'$M': 2,
'$D': 1,
'$W': 2,
'$H': 0,
'$m': 57,
'$s': 2,
'$ms': 0,
'$offset': 660,
'$u': false
}
For my application, I need to get the date that is in the $d field, like 2022-02-28, but when I do .format("YYYY-MM-DD") it returns 2022-03-01
Expected behavior
It should return 2022-02-28
Information
- Day.js Version [e.g. v1.10.8]
- OS: [Linux]
- Browser: Node 16
- Time zone: [CET]
Perhaps the documentation is not too clear (at least not the starting page for time zone).
You have to differentiate between
- 'parsing in time zone' (
dayjs.tz(isoString, 'Australia/Sydney')- gives always '2022-02-28T13:57:02+11:00') and - 'converting to time zone' (dayjs(isoString).tz('Australia/Sydney') - gives in time zone cet '2022-03-01T00:57:02+11:00' as cet = +02:00)
So just use the 'parsing' syntax and you get what you expected.