Question

Given an argument being passed in like this:

~/Documents/checkout/check-out.sh potatoes '2013/05/22 13:43:00'

Using 1st or 2nd line, produces

st=`date --date "$2" +%s`                   # mogul's suggestion
st=$(date --date="$2" +%s)                  # Kent's suggestion
st=$(date --date="2013/05/22 14:45:00" +%s)

date: illegal option -- -
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
    [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
Was it helpful?

Solution

OS X's version of date doesn't understand the --date option; you need to use -f and give it the format of the input date:

st=$(date -j -f "%Y/%m/%d %T" "$2" +%s)

For example:

$ date -j -f "%Y/%m/%d %T" "2013/05/22 14:45:00" +%s
1369259100

OTHER TIPS

Your OSX ships with a BSD date, not GNU date, so there are no GNU-style options (you can install GNU date via macports or homebrew).

x='2013/05/22 13:43:00'
st=`date --date "$x" +%s`

seems to work here. or in your case it should be

st=`date --date "$2" +%s`

try this:

st=$(date --date="$2" +%s)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top