Converting the weeknumber to a date and reversed in Linux

I hate it when people use week numbers, like "week 34". Week numbers are mostly not printed on any calendar or schedule. So; here is how to convert a date to a week number and a weeknumber to a date.

Converting the current date to a week number

This is an easy one, because the man page of date simply explains: %U - Displays week of the year(Sunday as the first day of the week) as a decimal number[00 - 53] . All days in a new year preceding the first Sunday are considered to be in week 0.

$ date +'%U'

Converting a specific date to a week number

A little harder, that why you don't have to figure it out yourself, just copy-paste and replace to meet your requirements:

$ date +'%U' 2009-10-2

Converting a week number to a specific date

This is not an easy one and requires a very nasty trick. To get a day in week 23 for example, use this command:

$ date --date="$(((23-$(date +'%U'))*7)) days"
Mon Jun 8 12:11:40 WEDT 2009

(See that 23 is the week number you'd like to get a date from.)

About Consultancy Articles Contact

References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning