Parse Date in Bash Parse Date in Bash bash bash

Parse Date in Bash


Does it have to be bash? You can use the GNU coreutils /bin/date binary for many transformations:

 $ date --date="2009-01-02 03:04:05" "+%d %B of %Y at %H:%M and %S seconds" 02 January of 2009 at 03:04 and 05 seconds

This parses the given date and displays it in the chosen format. You can adapt that at will to your needs.


This is simple, just convert your dashes and colons to a space (no need to change IFS) and use 'read' all on one line:

read Y M D h m s <<< ${date//[-:]/ }

For example:

$ date=$(date +'%Y-%m-%d %H:%M:%S')$ read Y M D h m s <<< ${date//[-: ]/ }$ echo "Y=$Y, m=$m"Y=2009, m=57


I had a different input time format, so here is a more flexible solution.

Convert dates in BSD/macOS

date -jf in_format [+out_format] in_date

where the formats use strftime (see man strftime).

For the given input format YYYY-MM-DD hh:mm:ss:

$ date -jf '%Y-%m-%d %H:%M:%S' '2017-05-10 13:40:01'Wed May 10 13:40:01 PDT 2017

To read them into separate variables, I'm taking NVRAM's idea, but allowing you to use any strftime format:

$ date_in='2017-05-10 13:40:01'$ format='%Y-%m-%d %H:%M:%S'$ read -r y m d H M S <<< "$(date -jf "$format" '+%Y %m %d %H %M %S' "$date_in")"$ for var in y m d H M S; do echo "$var=${!var}"; doney=2017m=05d=10H=13M=40S=01

In scripts, always use read -r.

In my case, I wanted to convert between timezones (see your /usr/share/zoneinfo directory for zone names):

$ format=%Y-%m-%dT%H:%M:%S%z$ TZ=UTC date -jf $format +$format 2017-05-10T02:40:01+02002017-05-10T00:40:01+0000$ TZ=America/Los_Angeles date -jf $format +$format 2017-05-10T02:40:01+02002017-05-09T17:40:01-0700

Convert dates in GNU/Linux

On a Mac, you can install the GNU version of date as gdate with brew install coreutils.

date [+out_format] -d in_date

where the out_format uses strftime (see man strftime).

In GNU coreutils' date command, there is no way to explicitly set an input format, since it tries to figure out the input format by itself, and stuff usually just works. (For detail, you can read the manual at coreutils: Date input formats.)

For example:

$ date '+%Y %m %d %H %M %S' -d '2017-05-10 13:40:01'2017 05 10 13 40 01

To read them into separate variables:

$ read -r y m d H M S <<< "$(date '+%Y %m %d %H %M %S' -d "$date_in")"

To convert between timezones (see your /usr/share/zoneinfo directory for zone names), you can specify TZ="America/Los_Angeles" right in your input string. Note the literal " chars around the zone name, and the space character before in_date:

TZ=out_tz date [+out_format] 'TZ="in_tz" in_date'

For example:

$ format='%Y-%m-%d %H:%M:%S%z'$ TZ=America/Los_Angeles date +"$format" -d 'TZ="UTC" 2017-05-10 02:40:01'2017-05-09 19:40:01-0700$ TZ=UTC date +"$format" -d 'TZ="America/Los_Angeles" 2017-05-09 19:40:01'2017-05-10 02:40:01+0000

GNU date also understands hour offsets for the time zone:

$ TZ=UTC date +"$format" -d '2017-05-09 19:40:01-0700'2017-05-10 02:40:01+0000