Show previous hour

date +%Y%m%d%H

output :
2011031415

I want to get previous hour like this 2011031414. Any one can help ?

If you GNU date then use

date -d'1 hour ago' '+%Y%m%d%H'

I am using SunOS LSMS 5.10 .

How about this?

perl -e '@dttime=(localtime(time - 60*60))[2,3,4,5];printf "%4d%02d%02d%02d\n",$dttime[3]+1900,$dttime[2]+1,$dttime[1],$dttime[0];'

how you show me how to store this result into a variable $target_date ?

##target_date=$(perl -e '@dttime=(localtime(time - 60*60))[2,3,4,5];printf "%4d%02d%02d%02d\n",$dttime[3]+1900,$dttime[2]+1,$dttime[1],$dttime[0];')
##echo $target_date
2011020219

Thank you. pravin27

you can play with timezone (see post #7 in this thread )

if running linux, also see

man zdump

I'm able to use this. but when my requirement is like. i'll give the time format like "2011-03-04-11" and in this case how to move back to "2011-03-04-10"

Not tested.

 
date -d'1 hour ago' '+%Y-%m-%d-%H'

You can modify the timezone (the TZ environment variable) to get arbitrary times. See the following example:

# (export TZ=GMT ; date)
Tue Apr  5 13:34:27 GMT 2011
# (export TZ=GMT+1 ; date)
Tue Apr  5 12:34:35 GMT 2011
# (export TZ=GMT-1 ; date)
Tue Apr  5 14:34:38 GMT 2011

I hope this helps.

bakunin

It could be better with not using the export command:

# TZ=GMT date
Tue Apr  5 13:34:27 GMT 2011
# TZ=GMT+1 date
Tue Apr  5 12:34:35 GMT 2011
# TZ=GMT-1 date
Tue Apr  5 14:34:38 GMT 2011

I'm not sure this syntax is portable but it helps modifying variables only for the following command. It works in bash and ksh

Actually the date format is accepted from the user. shouldn't take the present date.

ie if i given a date format like 2011-04-03-18 and i need to traverse two hours back 2011-04-03-16.
what should be the code to manipulate the date.

---------- Post updated at 08:50 AM ---------- Previous update was at 08:47 AM ----------

Actually here it takes the present date and traverse back but the date should be user defined. not he present date. can you help me out.

This is why i enclosed the commands in braces - "(......)" will open a subshell, execute everything between the braces in this subshell and then close it.

@Threadowner:

You might want to use "datecalc" by Perderabo, you can find it from the FAQ of the forum.

I hope this helps.

bakunin

Could this help you ?

TZ=GMT date -d '20010101 01:01:01 -0102'

It takes the date 20010101 01:01:01 and ADD 1 hour and 2 minutes. It's more a hack but it can be used for simple date manipulation.

It's been part of posix sh for quite a long while, so I would think it's safe to use.

My condolences to those forced to use shells which haven't implemented a common idiom standardized decades ago. :wink:

Regards,
Alister