4

Right now, 2014-01-06, GNU date 8.4 on CentOS release 6.4, does this:

$ date --date "last week" +"%Y-%V"
2013-01

On the other hand,

$ date --date "last week" +"%Y-%U"
2013-52

which is just marginally better.

Is this really the correct behavior?!

  • coreutils 8.13 date on Debian Wheezy does the same thing. – a CVn Jan 6 '14 at 15:11
4

I didn't want to step on @Jordanm's toes by editing up his answer too much but these additional sentences from the ISO week date he cited were also helpful in understanding the issue the OP has highlighted.

excerpt #1 - regarding ISO week dates

The system uses the same cycle of 7 weekdays as the Gregorian calendar. Weeks start with Monday. ISO week-numbering years have a year numbering which is approximately the same as the Gregorian years, but not exactly since an ISO week-numbering year (also called ISO year or week year informally) has 52 or 53 full weeks. That is 364 or 371 days instead of the usual 365 or 366 days. The extra week is referred to here as a leap week, although ISO 8601 does not use this term.

excerpt #2 - I also found this bit helpful in understanding the issue

The first week of a year is the week that contains the first Thursday of the year. It is also (equivalently) the week containing the 4th day of January.

Revisiting the date command

So if you take a look at the dateman page you'll notice this option in the formatting section.

   %G     year of ISO week number (see %V); normally useful only with %V

So when you want to print the year along with the ISO week (%V) you should really be using the %G for the year and not the typical %Y.

Example

So when my system's date is reportedly this:

$ date
Mon Jan  6 15:13:32 EST 2014

This command will report the ISO week date correctly, as you're looking for:

$ date --date "last week" +"%G-%V"
2014-01
|improve this answer|||||
  • @sds - your welcome! – slm Jan 6 '14 at 20:58
5

This is normal behavior for ISO week.

The ISO week-numbering year number deviates from the number of the Gregorian year on, if applicable, a Friday, Saturday, and Sunday, or a Saturday and Sunday, or just a Sunday, at the start of the Gregorian year (which are at the end of the previous ISO year) and a Monday, Tuesday and Wednesday, or a Monday and Tuesday, or just a Monday, at the end of the Gregorian year (which are in week 01 of the next ISO year). In the period 4 January to 28 December and on all Thursdays the ISO week-numbering year number is always equal to the Gregorian year number.

Source: http://en.wikipedia.org/wiki/ISO_week_date

|improve this answer|||||
  • 1
    Phew. That paragraph could use a little cleaning up, IMO... – a CVn Jan 6 '14 at 15:12
  • @MichaelKjörling Yes, it is a confusing read. Maybe citing the formula would have been better. – jordanm Jan 6 '14 at 15:14
  • 1
    thanks! is there a remedy? (how do I produce 2014-01 today?) – sds Jan 6 '14 at 15:53
  • @jordanm - hope you don't mind, not trying to steal your thunder. You found the info, I just reiterated it slightly differently 8-) – slm Jan 6 '14 at 20:16
  • @slm - nope, I don't mind. I had already given you an upvote. – jordanm Jan 6 '14 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.