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 7445121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:44:44+00:00 2026-05-29T11:44:44+00:00

Using bash date, I can get it to return a day of the week

  • 0

Using bash date, I can get it to return a day of the week relative to the current time.

date --d='last Sunday' #Returns date of the Sunday before today

I can also get it to return a day relative to some other date

date --d='02/1/2012 -2 days' #Returns date two days before Feb. 1, 2012

but how can I get it to return the day of the week relative to some non-current time? What I want to do is:

date --d='Sunday before 02/1/2012' #Doesn't work! I want Sunday before Feb. 1

If possible, I would even like to be able to chain strings so that I can reference relative days from the new date:

# Should return 2 days before the Sunday before Feb. 1, 2012
date --d='Sunday before 02/1/2012 - 2 days'

Though this chaining is not as important. Is there some way for bash date to return a day based on the relative day of the week?

  • 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-05-29T11:44:45+00:00Added an answer on May 29, 2026 at 11:44 am

    You can use a little day number arithmetic:

    base="02/1/2012"
    feb1_dayofweek=$( date -d $base +%w )
    target_dayofweek=0   # sunday
    date -d "$base - $(( (7 + feb1_dayofweek - target_dayofweek) % 7 )) days"
    

    result:

    Sun Jan 29 00:00:00 EST 2012
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the date(D, m/d/Y h:i:s A T) function call to get current date
I tried using $(date) in my bash shell script, however, I want the date
given a plain text file, how can I do, using bash, awk, sed, etc,
Is it possible to get the list of running Apps using Bash? I'm not
using bash, I'm trying to insert a variable for the date and search a
I am using bash shell on Solaris 5.10 .Daily I get some files having
What's the simplest one-liner to get the last commit date for a bunch of
Using bash, how can I search for all occurrences of the substring 'foo' in
Every time I run a script using bash scriptname.sh from the command line in
Trying to get started using phpunit and can't get it installed/setup properly. When I

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.