0

How to get the week number in linux using gawk with different first day of the week? the date command can give me the week number with +%V but it is based on Monday (1-53) or +%U (based on Sunday, 0-53).

I tried to to do this: date -d "ddmmyy+2days" +%V, but the result is not correct. I want the first day of the week is based on Saturday.

Thanks

EDIT: add the criteria The first day of the week in my place is Saturday, and the first week is depend on the number of the day to the nearest Saturday. If the current week have less than 4 days before the nearest Saturday, then it will be counted to the last year week (52/53). It is the same with the week in the end of the year.

2
  • be careful: the definiion of week-number (at least in business) might be tricky; for example, in many countries, week1 is defined as "the first week which has a thursday in it". Thus, week1 might end up being in the last year!.
    – blabla999
    Jun 24, 2011 at 8:15
  • yes, the first day of the week in my place is Saturday, and the first week is depend on the number of the day to the nearest Saturday. If the current week have less than 4 days before the nearest Saturday, then it will be counted to the last year week (52/53). It is the same with the week in the end of the year...
    – nandaka
    Jun 30, 2011 at 6:11

1 Answer 1

0

strftime (at least in gawk) can do that. E.g.:

awk '{print strftime("%U",systime()) ; exit }'

See the official documentation here: http://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html

2
  • The problem is: I want to get the week number of the date but with different first day of the week (for example: Saturday). The first day of the week for %U is Sunday, and %V is Monday.
    – nandaka
    Jun 30, 2011 at 6:08
  • Then it's and added if statement, e.g. if ( strftime("%A",systime()) == "Saturday" ) { print strftime("%U",systime())+1 } else { strftime("%U",systime()) } Jun 30, 2011 at 7:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .