Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>For GNU <code>date</code>:</strong></p> <pre><code>date_222days_before_TodayYear=$(date --date="222 days ago" +"%Y") date_222days_before_TodayMonth=$(date --date="222 days ago" +"%m") date_222days_before_TodayDay=$(date --date="222 days ago" +"%d") </code></pre> <hr> <p><strong>For BSD <code>date</code>:</strong>: </p> <p>If you are using OS X or FreeBSD, use the following instead because BSD date is different from GNU date:</p> <pre><code>date_222days_before_TodayYear=$(date -j -v-222d +"%Y") date_222days_before_TodayMonth=$(date -j -v-222d +"%m") date_222days_before_TodayDay=$(date -j -v-222d +"%d") </code></pre> <p>Source: <a href="http://www.unix.com/man-page/FreeBSD/1/date/" rel="noreferrer">BSD date manual page</a></p> <p><strong>Note:</strong></p> <p>In <code>bash</code> and many other languages, you cannot start a variable name with a numerical character, so I prefixed them with <code>date_</code> for you.</p> <hr> <p><strong>Second Update:</strong> New requirement - <em>Using 222 Working days instead of 222 Regular days:</em></p> <p>(<strong>Assumption:</strong> Not considering statutory holidays, because that just gets far beyond the scope of what I can help you with in a shell script:)</p> <p>Consider 222 working days:</p> <ul> <li>5 working days per week, that is <code>floor(222/5) == 44 weeks</code></li> <li><code>44 weeks * 7 days per week == 308 days</code></li> <li>Extra days leftover: <code>222 % 5 == 2</code></li> <li>Therefore <code>222 working days == 310 regular days</code></li> </ul> <p>But, there is a catch! If the number of regular days is <code>308</code> or some multiple of <code>7</code>, then we would have been fine, because any multiple of 7-days ago from a working day is still a working day. So we need to consider whether today is a Monday or a Tuesday:</p> <ul> <li>If today is a Monday, we'd get Saturday where we wanted Thursday</li> <li>If today is a Tuesday, we'd get Sunday where we wanted Friday</li> </ul> <p>So you see we need an additional offset of 2 more days if today is either Monday or Tuesday; so let's find that out first before we proceed:</p> <pre><code>#!/bin/bash # Use 310 days as offset instead of 222 offset=310 # Find locale's abbreviated weekday name (e.g., Sun) today=$(date -j +"%a") # Check for Mon/Tue if [[ "$today" == "Mon" ]] || [[ "$today" == "Tue" ]]; then offset=$((offset+2)) fi date_222_working_days_before_TodayYear=$(date -j -v-${offset}d +"%Y") date_222_working_days_before_TodayMonth=$(date -j -v-${offset}d +"%m") date_222_working_days_before_TodayDay=$(date -j -v-${offset}d +"%d") </code></pre> <p>And that should do it =)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload