Tinderbox Forum

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


(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.

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);

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