Your use of the information on Hobbs Knowledge Base is at your own risk and I accept no responsibility for any damage caused. More info here.

How To Print a Date In The Past and Future With Linux 'date' Command

From Hobbs Knowledge Base

Jump to: navigation, search

Summary

Image:Notepad.gif NOTE: Although this information depends on your specific version of the 'date' command, most versions installed by default in any Linux distribution do work correctly.

To get the date in the past or future (by a number of days), use one of the commands below, shown in red:

user@host:~> date
Fri Feb 29 10:36:26 GMT 2008
user@host:~> date --date='2 days'
Sun Mar  2 10:36:26 GMT 2008
user@host:~> date --date='2 days ago'
Wed Feb 27 10:36:26 GMT 2008
user@host:~>

Also, there are many ways to format the date command (see the date man page for your operating system), but one of the most useful i find is the one for entering dates into the DATE field in a MySQL database (which i'm sure has other uses too):

user@host:~> date '+%Y-%m-%d'
2008-02-29
user@host:~> date --date='2 days' '+%Y-%m-%d'
2008-03-02
user@host:~> date --date='2 days ago' '+%Y-%m-%d'
2008-02-27
user@host:~>

Image:Notepad.gif NOTE: Info on the 'date' command can be found on the date man page for your operating system, and more info on printing dates in the past and future can be found by using the 'info date' command on your system, if you have that particular documentation installed.


Image:Envelope.gif If the information on this page is either out-of-date, incorrect, or if you'd just like more information then please Contact Me.
Personal tools
Help