Compare DateTime against a point in time (in local time zone)

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!

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.

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.

1 Like

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.

1 Like

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

1 Like

@Kien,

I know you have a working expression, but here’s another that may also work for you. I can’t say it’s preferred, but it does shorten the code a bit.

Date.parse(Date.now().toLocaleDateString()).plusHours((Date.now() - (Date.parse(Date.now().toLocaleDateTimeString({"timeZone":"US/Eastern"})))) / 3600000)

I’m glad you brought this question to the community. I’ve had similar challenges in the past and I think having worked through this again today may help me clean up some very old code.

[Followup Edit]
This expression also handles both UTC- and UTC+ offsets