01-12-2022 11:35 AM
Hi, I’m having a hard time trying to figure out a way to compare a DateTime property (which is stored in UTC) to a point in time i.e. Today midnight (in the current local time zone which is US/Eastern -05:00)…
The closest thing came to find is Date.parse(Date.now().toLocaleDateString()+"T00:00:00.000-05:00")
which in turns gives me “2022-01-12T05:00:00.000 UTC”… but I don’t want to hard code the “-05:00” portion as during DST it would be -04:00…
Any ideas? Thanks!
01-12-2022 12:04 PM
Hey @Kien,
If you ever come across a use case where you need to add or remove hours from a certain datetime, there are functions specifically for doing that:
Date.now().plusHours(5)
Date.now().minusHours(5)
But as you said, in this case that would mean hardcoding the time difference.
That’s why you can do this by calculating the difference between EST and GMT which will take daylight saving into consideration, and from there you can manipulate the result by adding or removing hours to adjust the required difference.
Date.now().toLocaleDateTimeString({timeZone:"EST", format: "HH"}) - Date.now().toLocaleDateTimeString({timeZone:"GMT", format: "HH"}) -1
This expression will return -5 hours difference, which will be - 4 during daylight saving.
I hope you’ll find this helpful,
Regards,
Bojan.
01-12-2022 01:38 PM
Instead of using EST/EDT, you could use the Joda time zone alias, in this case "US/Eastern"
, to have the expression handle the offset automatically depending on the date.
01-12-2022 01:33 PM
Yeah – this is cumbersome at the moment, so hopefully can be improved down the line.
The best I could suggest is along the lines of what you were doing using the toLocaleDateTimeString. One of the things you can do is specify the time zone when you are converting to a String, which avoids having to figure out the offset yourself. Essentially something along the lines of this:
Date.now().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0).toLocaleDateTimeString({"timeZone":"US/Eastern", "format":"yyyy-MM-dd'T'HH:mm:ssZZ"})
which as of today returns the String "2022-01-11T19:00:00-05:00"
.
That should give you something you could then parse and compare with your other UTC values.
01-12-2022 02:08 PM
Thanks a lot everyone. All your replies have contributed to me crafting the right expression.
In the end I wanted to compare against midnight of today so I can filter and only process items that are created between now and midnight (last night) in the current timezone. Note that $orderDate is stored properly in UTC format.
The expression ends up being this (I know, not very elegant…):
Date.parse($orderDate) >= Date.parse(Date.now().toLocaleDateString()+“T00:00:00.000-0” + ((Date.now().toLocaleDateTimeString({timeZone:“US/Eastern”, format: “HH”}) - Date.now().toLocaleDateTimeString({timeZone:“UTC”, format: “HH”})) * -1) + “:00”)
This following expression would give me the proper date/time I needed (which is today at midnight in the -05/US-Eastern timezone) to compare my $orderDate against.
Date.parse(Date.now().toLocaleDateString()+“T00:00:00.000-0” + ((Date.now().toLocaleDateTimeString({timeZone:“US/Eastern”, format: “HH”}) - Date.now().toLocaleDateTimeString({timeZone:“UTC”, format: “HH”})) * -1) + “:00”)
Value at the time of this writting:
2022-01-12T05:00:00.000 UTC