Calculate the current Day number and Week Number

My sample app 'Tempus" is all about time and timing.

In response to a question from one of our users I’ve added the ability to calculate the day number and the week number.

Connect to API

Let’s start by connecting to the WorldClockAPI and store the server response in a variable called JSONResponse

Get the Ordinal Date

The ordinal date is expressed in the form YYYY-DDD where DDD is the number of days since January 1st.

In the example below we remove the year and hyphen by replacing them with an empty string.

So, in this example even though the variable is called ordinalDate it is in fact just storing the day number.

Convert to Week Number

Finally we can divide the number of days elapsed in the current year by the number of days in a week to get the week number. Since week number start at 1 rather than 0, don’t forget the +1 to avoid an off-by-one error.



If you want to use any of this in your own app, please feel free to remix my code:

1 Like