4

I need the number of the previous week of the month.

In the 2nd week of March, the previous week would be 1. In the 1st week of April, the previous week would be 5.

My week starts with Monday.

WEEK=$(( 1 + $(date +%V) - $(date -d "$(date -d "-$(($(date +%d)-1)) days")" +%V) ))

How may I subtract one from this so that I get the previous week (if the week equals 1 I have a exception, so I don't have to mind that).

It would also be nice if February, with only 4 weeks, is automatically recognized.

  • What number? Week of year? Week of month? Week since 1970-01-01 UTC? – terdon Apr 11 '17 at 12:20
  • the number should be Week of month – MadMax Apr 11 '17 at 12:22
  • 2
    Please edit your question and clarify then. Explain what you need exactly. Explain when you start counting (at 0 or at 1?) explain when a week begins (Sunday? Monday?). – terdon Apr 11 '17 at 12:23
  • I edited the question so that it asks for the "previous week" rather than the "last week". Please roll back if I'm totally wrong about this. – Kusalananda Apr 11 '17 at 12:46
  • no previous week is better your are 100% correct (when i started i wanted something a little bit different but now i changed my script so previous week is better) – MadMax Apr 11 '17 at 12:50
3

If I'm not mistaken, your expression (reorganized here a bit), finds the week number corresponding to the first day of the current month, the week number corresponding to today, and calculates the difference as week-of-month?

first=$(date -d "-$(($(date +%d)-1)) days")
weekofmon=$(( 1 + $(date +%V) - $(date -d "$first" +%V) ))

If that's right, and you want the week-of-month for last week, shouldn't it be enough to just replace "today" with "7 days ago" (in all places) to get the month-of-week corresponding to that date?

now="7 days ago"
first=$(date -d "$now - $(($(date +%d -d "$now" )-1)) days")
weekofmon=$(( 1 + 10#$(date +%V -d "$now") - 10#$(date -d "$first" +%V) ))

The relies on date being able to parse expressions like "7 days ago - 3 days". date +%V prints week numbers < 10 with a leading zero, which would cause them to be interpreted as octal numbers, breaking 08 and 09. Add the 10# to force bash to take the numbers in decimal.

A bit shorter way to get the first day of the same month:

first=$(date -d "$now" +"%Y-%m-01") 

(different format, but date should be able to interpret it.)

  • it seams to work !!!!! any idea on how to check this for other dates without having to manipulate the system time? – MadMax Apr 11 '17 at 12:54
  • 1
    @MadMax If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites. – terdon Apr 11 '17 at 12:54
  • @MadMax, note, updated, a minor problem with leading zeroes – ilkkachu Apr 11 '17 at 13:10

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.