17

I saw this date command:

date -d @1332468005 '+%Y-%m-%d %H:%M:%S'

Resulting in :

2012-03-22 22:00:05

How can I convert it back from 2012-03-22 22:00:05 to 1332468005 with bash shell?

Kusalananda
  • 333,661
Uncelvel
  • 345

3 Answers3

26

man date will give you the details of how to use the date command.

To convert long date in standard format into Unix epoc time (%s): date -d '2012-03-22 22:00:05 EDT' +%s

David
  • 622
9

MacOS - date -j -f "%Y:%m:%d %H:%M:%S" '2012:03:22 22:00:05' +%s

demmonico
  • 91
  • 1
  • 2
2

On BSD OS (FreeBSD, OpenBSD, NetBSD), date doesn't support -d flag. Use instead -j flag ("Parse the provided date and time and display the result without changing the clock") and strftime string format.

$ TZ=UTC date -j -f "%Y-%m-%d %H:%M:%S" "2012-03-22 22:00:05" +%s
1332453605
lcheylus
  • 193
  • This may have been better suited as an update to the already existing (and virtually identical) answer that deals with macOS? – Kusalananda Nov 11 '23 at 09:46