I'm using Luxon.js to handle Date and I'm trying to parse some ISO string which es from the server. It has this format
2019-04-04T12:12:07+03:00
and I'm using Luxon's method - fromISO which should parse this str
DateTime.fromISO("2019-04-04T12:12:07+03:00", "dd LLLL yyyy")
I'm expect to see 04 April 2019 - 15:12 in output but it returns 04 April 2019 - 12:12 somehow and I can't understand why it happens?
Am I doing something wrong? because when I'm trying to use this ISO string 2019-04-04T12:12:07.756Z it works like a charm. But I don't understand why the previous one isn't working and what should I do to make it working?
I'll appreciate any help!
P.S. to be honest, I have some gaps of my knowledge about ISO time so sorry if the question is stupid.
I'm using Luxon.js to handle Date and I'm trying to parse some ISO string which es from the server. It has this format
2019-04-04T12:12:07+03:00
and I'm using Luxon's method - fromISO which should parse this str
DateTime.fromISO("2019-04-04T12:12:07+03:00", "dd LLLL yyyy")
I'm expect to see 04 April 2019 - 15:12 in output but it returns 04 April 2019 - 12:12 somehow and I can't understand why it happens?
Am I doing something wrong? because when I'm trying to use this ISO string 2019-04-04T12:12:07.756Z it works like a charm. But I don't understand why the previous one isn't working and what should I do to make it working?
I'll appreciate any help!
P.S. to be honest, I have some gaps of my knowledge about ISO time so sorry if the question is stupid.
Share Improve this question asked Apr 4, 2019 at 12:23 VelidanVelidan 6,00414 gold badges57 silver badges92 bronze badges 5- 2 Your starting timestamp is in the "+3:00" time zone, three hours ahead of UTC. 12:12:07 in that timezone is 09:12:07 UTC. – Pointy Commented Apr 4, 2019 at 12:30
- Thanks for the answer @Pointy. Let's clarify it a bit. Let's imagine that my timezone is UTC+3. so is this string 2019-04-04T12:12:07+03:00 means that this record was done in 12.12 and I should add 3 hours (and it will be 15.12) or it means that it was created in 15.12 my time and the ISO just substract this 3 hours? and why this string 2019-04-04T12:12:07.756Z has been parsed correctly? – Velidan Commented Apr 4, 2019 at 12:41
- 2 Right, if you are in the +3:00 timezone, then creating the date gives you a UTC date 3 hours earlier. Converting that back to string form (apparently) uses the local time zone, so that adds the 3 hours back. (I agree that thinking about time zones is really confusing!) – Pointy Commented Apr 4, 2019 at 12:49
- yeah, you are right. It's a bit annoying. Thank you Pointy for your well detailed explanation. I still have question about Luxon parsing and why it works in one case but isn't work in different case but at least now I have clear vision how it can be parsed. Thanks you a lot. – Velidan Commented Apr 4, 2019 at 13:02
- @Velidan—your problem isn't the parsing, it's the formatting. As Pointy said, the time ponent of the timestamp is "12:12:07+03:00", that is, 12:12:07 in timezone +3. If you are in timezone +3, then the time will be "12:12:07", which is what you are getting. To get 15:12 you'd need to be in timezone +6. – RobG Commented Apr 5, 2019 at 8:30
1 Answer
Reset to default 72019-04-04T12:12:07+03:00
says "in a zone with an offset of +3, it is 12:12". It isn't telling Luxon to add 3 hours; it's providing the context in which the time was expressed. For example, because zones whose offset is +3 have local times 3 hours ahead of UTC (that's what it means), then in England it's 9:12. The point is that it's a specific time on the planet, and the +3 is just telling you what rule was used in turning that time into a string.
Luxon takes in all that information and figures out what time it really is, which in its conception of time is the number of milliseconds that have transpired since the beginning of 1970 in UTC. It can then translate that into any zone's local time. Of course, if your puter is in a zone whose offset is +3, then that local time is 12:12:
-> TZ=Europe/Moscow node
> DateTime = require("luxon").DateTime
[Function: DateTime]
> var dt = DateTime.fromISO("2019-04-04T12:12:07+03:00")
undefined
> dt.toLocaleString(DateTime.TIME_24_SIMPLE)
'12:12'
My puter is actually on the US east coast, so I see a very different local time:
-> node
> DateTime = require("luxon").DateTime
[Function: DateTime]
> var dt = DateTime.fromISO("2019-04-04T12:12:07+03:00")
undefined
> dt.toLocaleString(DateTime.TIME_24_SIMPLE)
'05:12'
That's because my offset is -4, which means the local time here is 7 hours earlier than the +3 in the original string.
With my puter in any zone, I could of course tell Luxon to express the date in any other zone:
> dt.toUTC().toLocaleString(DateTime.TIME_24_SIMPLE)
'09:12'
> dt.setZone("Europe/Moscow").toLocaleString(DateTime.TIME_24_SIMPLE)
'12:12'
> dt.setZone("Asia/Tokyo").toLocaleString(DateTime.TIME_24_SIMPLE)
'18:12'