Parse Date in Bash


Question

How would you parse a date in bash, with separate fields (years, months, days, hours, minutes, seconds) into different variables?

The date format is: YYYY-MM-DD hh:mm:ss

1
54
5/10/2016 2:04:00 AM

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.

63
2/10/2016 11:06:41 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon