88

On Linux you can convert a date like "2010-10-02" to a unix timestamp in shell script by

date -d "2010-10-02" "+%s"

Since Mac OS does not have the equivalent -d for date. How do you go about converting a date to a unix timestamp in a shell script.

8 Answers 8

143
date +%s

This works fine for me on OS X Lion.

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

2 Comments

Works in Linux as well (Ubuntu 10.04)
OP asks about converting a specific given date to a timestamp. date +%s will just use the current date.
45

man date on OSX has this example

date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"

Which I think does what you want.

You can use this for a specific date

date -j -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"

Or use whatever format you want.

2 Comments

ON OSX Terminal using bash shell I am unable to use the above command Ex: currDate=date +%Y%m%d date -j -f "%a %b %d %T %Z %Y" "${currDate}" "+%s"
@Jagdeep This'd work on OSX only if the Timezone specified is that of system's timezone. I am in PDT so in the above command, changing EDT wtih PDT worked for me.
18

date -j -f "%Y-%m-%d" "2010-10-02" "+%s"

2 Comments

This generates a different Unix Timestamp every time you run it though, which I don't think is what you want
@DrewDaniels True! Quote from the man: "Note that any date or time components unspecified by the -f format string take their values from the current time." So as the hours, minutes, and seconds are absent from this -f format, they are taken from the current time, which explains why you get a different result at each run with this command. The correct answer using this method should be date -j -f "%Y-%m-%d %H:%M:%S" "2010-10-02 00:00:00" "+%s"
5

date -j -f "%Y-%m-%d %H:%M:%S" "2020-04-07 00:00:00" "+%s"

It will print the dynamic seconds when without %H:%M:%S and 00:00:00.

2 Comments

Nice, thanks. If you man strftime, you'll see that "%F is equivalent to %Y-%m-%d" and "%T is equivalent to %H:%M:%S". So I think you can shorten your input_fmt to "%F %T" if you'd like.
This is the only thing that worked for my use case - the other answers weren't idempotent and generated different Unix timestamps on each run, because the hours, minutes, and seconds were being used from the current date.
4

I used the following on Mac OSX.

currDate=`date +%Y%m%d`
epochDate=$(date -j -f "%Y%m%d" "${currDate}" "+%s")

Comments

4

Alternatively you can install GNU date like so:

  1. install Homebrew: https://brew.sh/
  2. brew install coreutils
  3. add to your bash_profile: alias date="/usr/local/bin/gdate"
  4. date +%s 1547838127

Comments saying Mac has to be "different" simply reveal the commenter is ignorant of the history of UNIX. macOS is based on BSD UNIX, which is way older than Linux. Linux essentially was a copy of other UNIX systems, and Linux decided to be "different" by adopting GNU tools instead of BSD tools. GNU tools are more user friendly, but they're not usually found on any *BSD system (just the way it is).

Really, if you spend most of your time in Linux, but have a Mac desktop, you probably want to make the Mac work like Linux. There's no sense in trying to remember two different sets of options, or scripting for the mac's BSD version of Bash, unless you are writing a utility that you want to run on both BSD and GNU/Linux shells.

Comments

1

To convert a date in UTC to a Unix timestamp:

date -ju -f "%F %T" "2021-03-08 14:56:21" "+%s"

Comments

0

I wrote a set of scripts that provides a uniform interface for both BSD and GNU version of date.

Follow command will output the Epoch seconds for the date 2010-10-02, and it works with both BSD and GNU version of date.

$ xsh /date/convert "2010-10-02" "+%s"
1286020263

It's an equivalent of the command with GNU version of date:

date -d "2010-10-02" "+%s"

and also the command with BSD version of date:

date -j -f "%F" 2010-10-02 "+%s"

The scripts can be found at:

It's a part of a library called xsh-lib/core. To use them you need both repos xsh and xsh-lib/core, I list them below:

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.