Add and subtract time
What if you want to communicate with users about not only the current date/time, but dates in the past or future?
For example, if you want to remind the user to do something two weeks from now, you’ll need to know what date that is.
You can create a new variable that takes the current time and add or subtract from it, like so:
In this example, you just come up with a name for your variable (e.g. “two_weeks_from_now”), and then equal that to be calendar::now (which gets the current date and time) + 2.weeks (which adds two weeks).
You could instead add 3.days, or 5000.seconds, as long as you add a period in between the number and the time unit.
Here are all the time units you can manipulate:
.seconds
.minutes
.hours
.days
.weeks
.months
.years
You can similarly add or subtract units like seconds/minutes/hours from calendar::time (the current time), or days, weeks, etc., from calendar::date (the current date, without the current time).
You can also add or subtract from something other than the present.
For example, let’s say your user will be working on a new habit for 21 days, starting on a day of their choosing, and you want them to mark their calendar when the 21 days is up.
You could do something like this:
In this example, you’re not looking to add or subtract from calendar::now (the present time), you’re adding or subtracting from a different calendar-based variable (“habit_start”).
|