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


Excellent idea! Here's

Excellent idea!
Here's expanded version that parses a year and a day of the week as well:
date -d "$year-07-01 +12 hour +$((($week-$(date +'%V' -d $year'-07-01'))*7+$day-$(date +'%u' -d $year'-07-01'))) days"

It is indeed, very tricky.

It is indeed, very tricky. But the most important thing is to be able to learn it which can be use in many projects. - Morgan Exteriors

How about this one: $ date

How about this one:

$ date [email protected]
Sat Feb 14 00:31:30 WEST 2009


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 [email protected]