Add minutes in Datetime Variable in Unix

Hi,

New to scripting.
I have datetime as a String and I need to add some hours/minutes in to that.

21/10/2009 23:00:00

After adding 180 minutes; it will look like this

22/10/2009 02:00:00

Regards,
Vikas Mahajan

convert to epoch time, add hours/minutes, convert back.
Search forums for the 'epoch' time conversion threads.

  1. In perl scripting it is very simple as you have lot of modules in CPAN such as Date::Manip, Date::Calc and so on.

  2. In shell, if your date supports and you are looking for 3 hours after from the current time then, this will help you...

$ date
Wed Oct 21 22:11:57 IST 2009
$ date --date='3 hours'
Thu Oct 22 01:11:59 IST 2009

Instead of fetching the current date time, I need to add the reqd. minutes in some variable datetime which i m gtng 4m somewhere else.

The code mentioned above will add the minutes inthe current system date.

Also i need todo this in HPUX Server.

---------- Post updated at 12:38 PM ---------- Previous update was at 11:54 AM ----------

Hi

Can any one provide me the script for the query raised by me.

I am trying for the same from last 6 to 7 hous.

Thanks & Regards
Vikas Mahajan

What about the other suggestion ?

Converting the time to epoch, and adding the required time, and reconverting epoch to DATE & Time ?!

#!/bin/ksh
epoch_seconds=$(perl -e 'use Time::Local; print timelocal(0,25,1,21,10,2009), "\n";')
echo $epoch_seconds
RESULT=`expr $epoch_seconds + 12600`
echo $RESULT
perl -e 'print scalar(localtime($RESULT)), "\n"'


Thu Jan  1 05:30:00 1970

why it is giving me wrong date.********

---------- Post updated at 11:30 PM ---------- Previous update was at 11:14 PM ----------

$ #!/bin/ksh
$ epoch_seconds=$(perl -e 'use Time::Local; print timelocal(0,25,1,21,10,2009), "\n";')
$ echo $epoch_seconds
1258746900
$ RESULT=`expr $epoch_seconds + 12600`
$ echo $RESULT
1258759500
$ perl -e 'print scalar(localtime('$RESULT')), "\n"'

Sat Nov 21 04:55:00 2009

******************************************

I got the result

But i need this in the format mentioned below . Could ny 1 help

21/11/2009 04:55:00

---------- Post updated 10-22-09 at 12:00 AM ---------- Previous update was 10-21-09 at 11:30 PM ----------

 
$ #!/bin/ksh
$ epoch_seconds=$(perl -e 'use Time::Local; print timelocal(0,25,1,21,10,2009), "\n";')
$ echo $epoch_seconds
1258746900
$ RESULT=`expr $epoch_seconds + 12600`
$ echo $RESULT
1258759500
$ perl -e 'print scalar(localtime('$RESULT')), "\n"'

Sat Nov 21 04:55:00 2009

******************************************

I got the result

But i need this in the format mentioned below . Could ny 1 help

21/11/2009 04:55:00

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Change the last line in your script to,

perl -e ' use POSIX qw(strftime); print strftime "%e/%m/%Y %H:%M:%S", localtime('$RESULT');';

Like thegeek said, you can use relative dates with e.g. GNU date. For example:

$> date -d "$(echo "21/10/2009 23:00:00"|awk -F/ '{OFS="/";print $2,$1,$3}') 3 hours" +'%d/%m/%Y %H:%M:%S' 
22/10/2009 02:00:00

Thanks for the help.

But this doesn't works on HPUX

---------- Post updated at 01:16 AM ---------- Previous update was at 01:15 AM ----------

This one is working.

Thanks for the help

---------- Post updated at 02:30 AM ---------- Previous update was at 01:16 AM ----------

date -d "$(echo "21/10/2009 23:00:00"|awk -F/ '{OFS="/";print $2,$1,$3}') 3 hours" +'%d/%m/%Y %H:%M:%S'

22/10/2009 02:00:00

This is not working on SUn Solaris