Pages

Thursday, January 03, 2008

How to get yesterday time

Sometimes, we need to create a simple script in linux which involves retrieving system time several days/hours/minutes ago.

Belows are example use of date command:

To get yesterday time:
$ date --date="1 days ago"
or
$ date --date="-1 days"


To get one month ago:
$ date --date="1 months ago"
or
$ date --date="-1 months"


To get one hour ago:
$ date --date="1 hours ago"
or
$ date --date="-1 hours"


To get one minutes in the future:
$ date --date="+1 minutes"


To get one seconds in the future:
$ date --date="+1 seconds"


We can also combine it, example to get one month and two hours ago:
$ date --date="1 month 2 hours ago"
or
$ date --date="-1 month -2 hours"

There are many other ways to express the particular time.

That's all :)

1 comment: