Days Until Specific Date

Could someone point me in the right direction?

I have a variable that is set to a date, such as 31-08-2024. I want to set another variable that calculates how many days there are from now() until the target date (31-08-2024)

Thanks!

Edit: I managed to sort it out. I converted the dates into seconds, deducted one from the other, and then /86400.

Thanks for sharing! For future reference, the following help article covers the date functions that are available in expressions:

So you could use the date() function to parse the date. Since it’s not in ISO format, you would need to pass in the format as the second parameter as noted in the docs.

#// $myDate = "31-08-2024"
duration = date($myDate, "DD-MM-YYYY") - now()
round(durationAs(duration, "days"))

As of today, 2024-08-03, that would output 27. Note that I wrapped it in round() otherwise it would include the fractional portion of the days.

Alternatively, you could get a human readable version of the duration using:

humanizeDuration(duration)       #// "a month"
humanizeDuration(duration, true) #// "in a month"

It’s a nice convenience method as it supports things like “a month ago”, “in a month” automatically, but it comes with its own opinionated cutoff points, so if you want full control over the output then you would want to come up with your own math / formatting logic.

Yep, I did try that.

I’m not sure if it’s the date settings on my devices, if less than 13 hours or more than 13 hours gives a different value (before 1 PM (27), after 1 PM (26)).

I ended up abandoning my rule and transferred it to Tasker to run directly from Google Calendar.

That makes sense as there are two challenges you are probably running up against:

  1. The timezone is ambiguous when using date() with just the date component and a custom format string (hotfix pushed)
  2. now() is the complete current date and time

1. Timezone Ambiguity

I pushed a hotfix that should improve this for your time format, but here’s the reasoning if you’re interested…

Timezone Ambiguity Details

When you are parsing using date() with just the date component in the input string and a custom format string, the timezone is ambiguous.

dt = date("2024-08-05")
formatDate(dt)  #//2024-08-05T00:00:00-05:00

dt = date("05-08-2024", "DD-MM-YYYY")
formatDate(dt) #//2024-08-04T19:00:00-05:00
  1. Note that in the first case, we used standard ISO formatting and the parser assumed it was parsing the raw date in my timezone

  2. Whereas in the second it thought it should parse it in UTC and then offset to my timezone.

Hotfix Deployed: Improved Timezone Disambiguation

I pushed out a hotfix which should better disambiguate the timezone using the date format you supplied, so this should work better now.

In other cases, you could explicitly set the timezone to UTC at the top of the expression to force all parsing in UTC, but that could complicate things by making now() report in UTC, so you would need to adapt for that.

2. Comparing to now() which has time

The now() function includes both the date and the time that it currently is. So if you want to compare just dates to dates, you would need to ensure the times are standardized (eg. reset to 00:00)

Reset ‘time’ component of now()

If you were purely looking for the date comparison excluding the time, you could use the startOfDate() command to ensure you were comparing the same time across both dates.

Raw now() includes the time:

dt = now()
formatDate(dt) #// 2024-08-05T11:37:12-05:00

Use startOfDate() to reset time:

dt = now()
dt = startOfDate(dt)
formatDate(dt) #// 2024-08-05T00:00:00-05:00

 
 

Full Example - Date Only Comparison

parsedDate = date("31-08-2024", "DD-MM-YYYY") #//2024-08-31T00:00:00+00:00
today = startOfDate(now())
duration = parsedDate - today
formattedDuration = durationAs(duration, "days")