Extract weeknumber of year from given date

i need to extract week number from a given date
input = 01-04-2021
output = 14

input = 26-12-2021
output = 1

note here week start day is Sunday, week end day is Saturday.

how can i achieve this

If you are on a Linux system it should be relatively easy to parse the output of the cal command.
Must admit don’t understand how 26-12-2021 and 1 relate.

Be well, Mike

I’m new to Ruby but I see the time class has wday and yday so if you get the wday (weekday) of Jan 1 of the year in question then you know when the first Sunday is. yday of the date in question divided by 7 gives the number of weeks into the year and minus the 1 Jan and the first Sunday ought to pin down the week number.

Should also be worth checking the date class.

Do hope this isn’t a homework assignment,
Mike

Take a look at the documentation for class Time - RDoc Documentation
strftime has different week number options.

1 Like