Working with Dates
CreatiCode last edited by
In the “Sensing” category, there are some new blocks provided by the CreatiCode playground to work with dates.
You can use the “date” block to represent a date:
The input can take 2 formats:
- MM/DD/YYYY: for example, “11/05/2018”
- YYYYMMDD: for example, “20181105”
Both formats will represent the same date. You can click on this block to verify it:
You can use the operator blocks to run basic calculations on dates.
For example, you can use the add and subtract blocks to calculate the date that’s some days before or after a date:
You can also use the subtract block to calculate the number of days between 2 dates:
These 4 reporter blocks take the date block as the input, and tell us which day of the week/day/month/year is that date:
Note that for “day of week”, the value 1 means it is a Monday, and the value 7 means it is a Sunday.
For example, suppose we want to know the first Monday after the date August 1st, 2023. We can calculate that date in 3 steps:
- We need to find out the day of week for 20230801, which is a Tuesday (value of 2)
- We need to calculate how many days are there until the following Monday, which is 6 (8 minus 2)
- We need to calculate the date that is 6 days after 20230801, which is 20230807.
Here are the blocks for the calculation in each step: