Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:45:47+00:00 2026-06-14T18:45:47+00:00

I need to create three variables, each for Year, Month, and Day for Today’s

  • 0

I need to create three variables, each for Year, Month, and Day for Today’s date, minus X number of days. For this question I’ll choose a random amount of days: 222.

So if:

TodayYear=`date +%Y`
TodayMonth=`date +%m`
TodayDay=`date +%d`

What I want is 222 days before this.

222days_before_TodayYear=???
222days_before_TodayMonth=???
222days_before_TodayDay=???

Edit: Need 222 working days instead 222 regular days.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:45:48+00:00Added an answer on June 14, 2026 at 6:45 pm

    For GNU date:

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

    For BSD date::

    If you are using OS X or FreeBSD, use the following instead because BSD date is different from GNU date:

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

    Source: BSD date manual page

    Note:

    In bash and many other languages, you cannot start a variable name with a numerical character, so I prefixed them with date_ for you.


    Second Update: New requirement – Using 222 Working days instead of 222 Regular days:

    (Assumption: Not considering statutory holidays, because that just gets far beyond the scope of what I can help you with in a shell script:)

    Consider 222 working days:

    • 5 working days per week, that is floor(222/5) == 44 weeks
    • 44 weeks * 7 days per week == 308 days
    • Extra days leftover: 222 % 5 == 2
    • Therefore 222 working days == 310 regular days

    But, there is a catch! If the number of regular days is 308 or some multiple of 7, 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:

    • If today is a Monday, we’d get Saturday where we wanted Thursday
    • If today is a Tuesday, we’d get Sunday where we wanted Friday

    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:

    #!/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")
    

    And that should do it =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an unknown number of python variables, based on a list
I need to create a method to generate a unit vector in three dimensions
I have the following three arrays and need to create a new two-dimensional array
In my constructor, I want to create a random color. Therefore, I need three
I need to create custom styled select drop downs (there are three) form inputs.
I need to create a function which can be executed only once, in each
I've created a three step order form and I need help with the third
I need to create a product catalog, in tree type. every tree node presents
We need to create a basic PDF reader running on J2ME. While there are
I need to know if there is a way to create a custom shortcut

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.