Getting the week of the year from a date

I find myself wanting to pull the week of the year from a date attribute. For example, as I write these words, this is week 2 of the year 2018. While the format function offers many codes, I just want to confirm that it does not support that one. My plan is to use runCommand() and use the Unix date command to do this, but before I start figuring that out, thought I would check if there is an easier way.

No, there is a Date.weekday, that returns the numerical day of the week but no such built-in feature for week-of-the-year - most likely as I donā€™t recall anyone ever asking for one before now! A summary of formatted date/time component codes is here.

Your runCommand solution sounds the best approach for now.

1 Like

This doesnā€™t work for the first 3 days of a week, but you can write action code to check for that and add +1

$Weeks=ceil(days($BaseDate,$MyDate)/7);

(Iā€™m using BaseDate to stand for the date from which to count weeks ā€“ i.e., January 1 of a year.)

1 Like

This would be fairly easy to add if thereā€™s anything like general need.

1 Like

However, days(date1,date2), hours(date1,date2), etc., give the integer number of the given date/time unit between date1 and date2.

I think a more consistent approach would be Date-type property Date.weekOfYear.

No gainsay implied in my last :grinning:, but simply that it reminded me of discussion when Date.weekday was added - i.e. issues of whether Sunday or Monday is day #1, plus locale differences, etc. I know dates get complex - and Iā€™m not an expert. This might help explain the difference I allude to above. My (limited) understanding is the week-of-the-year is a more business/giv concept and probably derives from things like payroll calculation.

Thus, 1 Jan is not necessarily the beginning of the first week of the year and so if weeks(1/1/2018,today) gives the differing number of days divided/7 (I presume) thismay not always mesh with the first week of the year. 1 Jan will (should!) always be in the first week of the year, but 7 Jan may not be so. My presumption was weeks(date1,date2) would give you the integer week different whilst Date.weekOfYear would work based on the current localeā€™s first week of the year. The two may be the same, but not always. I guess it depends which ā€˜weekā€™ definition one is after.

FWIW, if weā€™re adding more date-based utilities Iā€™d go for both weeks() and Date.weekOfYear so the user has the choice of appropriate calculation.

My actual use case is to assign ā€œweek numberā€ to dates in a course schedule. Not a ā€œmust haveā€ feature for me, since I donā€™t have to have this in my ā€œteaching gridā€ and could do it in other ways. For example, @PaulWalters approach would actually work well for me.

In this case, if $StartDate(/config/2018/FallTerm) is understood to be the start of the semester, ceil(weeks($MyDate, $StartDate(/config/2018/FallTerm)) should be what you want, more or less

1 Like

I am not getting the weeks function to work. should I use days and divide by 7?

This works:

$WeekNo = 1 + floor(days($StartDate("/TeachingGrid/Config"),$EndDate)/7);

1 Like

At present there is no weeks() action code. Essentially weā€™re discussing a putative new feature.

As youā€™ve shown, days/7 gives a similar result, especially if itā€™s just for weeks from a given start date (term?, semester?). The point MB was making using a date stored in a note is that multiple notes might run this action so it helps that the start date is referenced from a common canonical source rather than be set in each note.

1 Like

My bad. I see now that you were proposing weeks() and MB was giving an example of how it would be used. All clear now.

1 Like

I have a similar use case in which my Tinderbox document has a Timeline container which includes 365 notes - one for each day of the year in yyyy-mm-dd format; I use these for daily journal notes.

Iā€™d like to further group these by week without the work of creating 52 week notes, so I want to calculate a ā€œweekYearā€ numeric attribute that I can use with action code and create a useful linked view.

For 2023 at least, this attribute calculation works:

// Calculate the week of the year

$weekYear=floor(days(ā€œ2023-01-01ā€,$StartDate)/7)+1;

I wouldnā€™t have stumbled on this without this great thread in the Forum! My gratitude ā€¦

1 Like

I would use this. VERY helpful or project management and general activity tracking.

In which case I think youā€™ll also like Date.week()ā€”see the linked article for why the week number range is 1ā€“53 and not 1ā€“52 (hint: alignment of week boundaries with year end).

Currently, to get the week number you are using:

$MyNumber=floor(days("2023-01-01",$StartDate)/7)+1;

which is fine, but note that it can also can be done even more succinctly as:

$MyNumber=$StartDate.weeks;

Or, to get the current week without needing to save a date:

$MyNumber = date("now").week;

Donā€™t worry, as both work so donā€™t rush to change anything. In the screen grab below are the results of all 3 codes above with the code run as stamps. The $StartDate is 31 July 2023; Iā€™m in UK so dates format in Displayed Attributes as dd/mm/yyyy. Your original example is output to $MyNumber and the next to the $MyNumberA and $MyNumberB:

Here is the test file I used: get-weeknumber-1.tbx (104.0 KB)

Also take a look at Date.weekday(), and see a listing of Date-time operators.

3 Likes