18

The date function on OS X (Snow Leopard) does not have the --date option like the GNU version and I am not able to figure out how to get the equivalent of the following on OS X:

startdate=2010-01-01
enddate=2010-01-31
foldate="$startdate"

until [ "$foldate" == "$enddate" ]
do
    # do something with the date here - like pass it as a parameter to a command
    foldate=`/bin/date --date "$foldate 1 day" +%Y-%m-%d`
done

SOLVED with answers from SiegeX:

startdate=2010-01-01
enddate=2010-01-31

sDateTs=`date -j -f "%Y-%m-%d" $startdate "+%s"`
eDateTs=`date -j -f "%Y-%m-%d" $enddate "+%s"`
dateTs=$sDateTs
offset=86400

while [ "$dateTs" -le "$eDateTs" ]
do
  date=`date -j -f "%s" $dateTs "+%Y-%m-%d"`
  printf '%s\n' $date
  dateTs=$(($dateTs+$offset))
done
3
  • Glad to see that working for you. I'm not sure what version of bash OSX has (if it even has bash), but if it does you should wrap your commands in $() instead of backticks like sDateTs=$(date -j -f ...) and use double brackets [[ instead of a single [ so you can do while [[ "$dateTS" < "$eDateTs" ]]
    – SiegeX
    Commented Dec 20, 2010 at 17:46
  • What is the purpose of the offset? Why is it needed? Commented Mar 26, 2018 at 16:39
  • Note that it's safer to use = than == in [, as use of == is an extension not all baseline-POSIX shells support, whereas = is defined by the POSIX standard for test. Commented Dec 24, 2019 at 20:54

1 Answer 1

1

Read your date's manpage by running man date and look at the -v option. Here is an excerpt:

-v Adjust (i.e., take the current date and display the result of the adjustment; not actually set the date) the second, minute, hour, month day, week day, month or year according to val. If val is preceded with a plus or minus sign, the date is adjusted forwards or backwards according to the remaining string, otherwise the relevant part of the date is set. The date can be adjusted as many times as required using these flags. Flags are processed in the order given.

5
  • 1
    SiegeX, I did that, but cannot figure out how I would change anything other than the output of date with that option. In other words, I can add a day to "now" with date -j -v+1d, but how would I add a day to an arbitrary day? Commented Dec 14, 2010 at 0:54
  • The problem with -v is "take the current date ..." (from the manpage and above). I don't want to modify the current date, I need to modify an arbitrary date - one that I specify. Commented Dec 14, 2010 at 3:39
  • @hglattergotz looks like the only thing you can do is to save the current date to a variable then set the date to the $foldate and use -v to get the offset from that. Finally set the date back to it's original value
    – SiegeX
    Commented Dec 14, 2010 at 6:57
  • 2
    @hglattergotz actually, another option is to convert your offset values into seconds then convert your $foldate into epoch via -j -f "%Y-%m-%d" $foldate "+%s". Add that epoch value to your offset in seconds and convert that back into a date via -j -f "%s" $epoch_plus_seconds "+%Y-%m-%d"
    – SiegeX
    Commented Dec 14, 2010 at 7:01
  • @SeigeX, thanks for the last hint. Got it working now. Will post example as separate answer since I cannot seem to format it properly in a comment. Commented Dec 20, 2010 at 15:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.