44

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible.

Given that caveat, how does one do date math in a bash script on OS X? I've seen a post (on another site) where someone did the following:

date -d "-1 day"

But this does not seem to work on OS X.

Addendum:

Several people have commented and responded that Ruby, Python, Perl, and the like come standard with OS X. I'm familiar with all three of these languages and could easily write a script that does what I want. As a matter of fact, I already have such a script, written in Ruby.

So perhaps I should clarify what I mean by 'external dependency'. What I mean is, I don't want my bash script to have to call any other script external to it. In other words, I want it to use some utility available in a vanilla installation of OS X and already on the path.

However, it doesn't look like this is possible, so I will have to make due with my external dependency: a Ruby script.

1
  • ruby, perl and python all come standard with OS X, so you don't have to worry about the dependency Commented Jan 31, 2009 at 7:13

8 Answers 8

56
$ date -v -1d

-d sets Daylight Savings time flag.

Try man date for more info.

Sign up to request clarification or add additional context in comments.

1 Comment

date -v -1d +%s to get it in unix timestamp
30

For example, this OS X date command will add 14 days to the input string 20160826 (not the present time):

date -j -f %Y%m%d -v+14d 20160826 +%Y%m%d

1 Comment

for me quotes are needed: date -j -f '%Y%m%d' -v+14d 20160826 +%Y%m%d
25

Because MacOS is based on BSD, not linux, the command is actually:

date -v-1d

The adjust switch takes the format -v[+/-](number)[ymwdHMS].

Comments

1

The command Matthew mentions works for me with /bin/date on Leopard. Besides, as Schwern points out, if you're sure your shell script is going to be run on Mac OS X, why don't you want to use Python or Ruby, that both come in standard in /usr/bin? This would in no way affect portability in your particular scenario.

Comments

0

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible.

Doing in C or as a Ruby script would result in less dependancies, and be more cross-platform.. Bash-scripting just calls various other scripts/applications, mainly the GNU utilities.. Many of the commands are unavailable, or take different arguments on other platforms (especially between OS X and Linux)..

Ruby is included by default on OS X Leopard (and 10.4, and probably previous versions if I recall correctly), and if you use Rubys built-in date libraries, it will work consistently on any platform.

Whenever you try to do anything remotely complicated, you are generally best of using a "proper" scripting language!

Comments

0

Here is a script that I use to print the dates for my journal in google doc's:

    #!/bin/bash

    # ask user for variable details
    read -p 'Starting date in "mm-dd-yyyy" format: ' DATE

    # adjust the count by editing the "x" number in {0..x}
    # 91 days will give you about 3 months, currently set to 6 for testing
    # %a gives short day of the week, -m -d gives single digit months and days, capital Y gives 4 digit year

    for i in {0..6}
    do
         NEXT_DATE=$(date -j -f %m-%d-%Y -v+"$i"d $DATE +'%a %-m/%-d/%y')
         echo -e "$NEXT_DATE\n\n\n"
    done

Here is the sample output, (I use 3 new lines between each date):

Sun 4/1/18

Mon 4/2/18

Tue 4/3/18

Wed 4/4/18

Thu 4/5/18

Fri 4/6/18

Sat 4/7/18

Comments

0

You might be looking for this. gdate '+%s' -d '2 weeks ago' or gdate '+%s' -d '1 day ago'

These two examples return in the Epoch time. I turn those seconds in to something my language can read normal strptime(seconds, '%s'). Python Ruby and Javascript are the few I know have it. I am sure Java and all the others do as well.

Also gdate is date (GNU coreutils) it just called something different on mac. Strange thing is on my other mac it let me use just data to get this same off set. Don't know why might be they change something recently.

Comments

0

I used the following on, macOS Mojave 10.14.5, to find the DHCP renewal time open

date -v +`ipconfig getoption en0 renewal_t1_time_value`S

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.