jdamae
jdamae

Reputation: 3909

date calculations using unix time

Looking for help in unix to get the following date stamps using unix date:

today at 12:00am  
12:00am - 15 days ago
12:00am - 30 days ago 

in Unix epoch time? (1300295838)

Upvotes: 0

Views: 6038

Answers (4)

cmollis
cmollis

Reputation: 147

to calculate the date for YESTERDAY:

time is the current time in seconds(the number of seconds in a day)

dateYYYYMMDD=`perl -MPOSIX -le 'print strftime "%Y%m%d", localtime(time-60*60*24);'`

time +/- calculate the seconds for your desired date after or before the current time in seconds.

Upvotes: 1

ewan.chalmers
ewan.chalmers

Reputation: 16235

Today at 12:

mydate="`date +%D` 12:00"

To do date arithmetic:

date -d "$mydate 15 days ago"

To get epoch time:

date +%s

To put together in oneliner:

date -d "`date +%D` 12:00 15 days ago" +%s

Upvotes: 3

dogbane
dogbane

Reputation: 274572

You can use a perl library such as DateTime to do this quite easily.

For example, the following code creates a new date my subtracting 15 days from today and then calls the epoch function:

use DateTime;

my $dt = DateTime->now
                 ->subtract(days => 15)
                 ->set_hour(0)
                 ->set_minute(0)
                 ->set_second(0);

print $dt->epoch(),"\n";

Upvotes: 0

Christopher Armstrong
Christopher Armstrong

Reputation: 7953

You can use date -d on the shell to have it display the time you specify, not the current time. For time formatting characters, see: http://unixhelp.ed.ac.uk/CGI/man-cgi?date

Upvotes: 0

Related Questions