Working with Dates
-
Description
In the “Sensing” category, there are some new blocks provided by the CreatiCode playground to work with dates.
The “Date” Block
You can use the “date” block to convert a text input into a “Date” object:
The input may take any of these 5 formats:
Date Calculations
Once you get the Date object, 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:
Getting the Day of Week/Day/Month/Year of a Date
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.
Example - The First Monday After Some Date
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:
Days Since 2000 for Date
You can calculate the number of days since the date of Jan 1st, 2000. This is useful if you need to quickly find out the number of days between 2 dates. For example, there are 10 days between these 2 dates: