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.
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.)
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 , 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
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.
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.
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 ā¦
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.