Last Updated: March 02, 2016
·
376
· b4hand

Useful uses of the GNU date(1) command

I've seen lot's of people use an online converter tool to convert POSIX timestamps to a date string or to get a POSIX timestamp at a specific date; however, GNU's version of date can do both of these directly from the command line.

Getting the current time in POSIX timestamp format:

$ date %+s
1446491202

Converting an arbitrary POSIX timestamp into a human readable date string:

$ date -u -d @1446491202
Mon Nov  2 19:06:42 UTC 2015

But date can do some even more powerful things than this, you can specify arbitrary date strings using the -d flag in a mostly human readable format. For example, let's say you want to know the POSIX timestamp for the beginning of the day, 30 days ago at midnight UTC:

$ date -u -d '30 days ago 00:00' +%s
1443830400

And sure enough, that is the date we would expect:

$ date -u -d @1443830400
Sat Oct  3 00:00:00 UTC 2015

You can also find out dates in the future:

$ date -u -d 'next Fri'
Fri Nov  6 00:00:00 UTC 2015