Visit Our UNIX and Linux User Community


Add minutes in Datetime Variable in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add minutes in Datetime Variable in Unix
# 1  
Old 10-21-2009
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
# 2  
Old 10-21-2009
Quote:
Originally Posted by vikas482
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.
# 3  
Old 10-21-2009
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...


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

# 4  
Old 10-21-2009
Quote:
Originally Posted by thegeek
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...


Code:
$ 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
# 5  
Old 10-21-2009
What about the other suggestion ?

Converting the time to epoch, and adding the required time, and reconverting epoch to DATE & Time ?!
# 6  
Old 10-22-2009
Code:
#!/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 ----------


Code:
$ #!/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 ----------

Quote:
Originally Posted by vgersh99
convert to epoch time, add hours/minutes, convert back.
Search forums for the 'epoch' time conversion threads.
Code:
 
$ #!/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
Image

Last edited by zaxxon; 10-22-2009 at 02:03 AM.. Reason: code tags
# 7  
Old 10-22-2009
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 [code] and [/code] 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

Previous Thread | Next Thread
Test Your Knowledge in Computers #955
Difficulty: Medium
Modern antivirus programs cannot detect virus-patterns inside ordinary programs by scanning them for so-called virus signatures.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file for the last 5 minutes of contents every 5 minutes

Hi all, System Ubuntu 16.04.3 LTS i have the following log INFO 2019-02-07 15:13:31,099 module.py:700] default: "POST /join/8550614e-3e94-4fa5-9ab2-135eefa69c1b HTTP/1.0" 500 2042 INFO 2019-02-07 15:13:31,569 module.py:700] default: "POST /join/6cb9c452-dcb1-45f3-bcca-e33f5d450105... (15 Replies)
Discussion started by: charli1
15 Replies

2. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

3. Shell Programming and Scripting

How to monitor some UNIX process and send notification in every 10 minutes?

Hi Unix Members, Can anyone guide me to write one shell script to monitor the attach screen processes and when interrupted mail us. , like the processes - /bin/ciserver , /bin/clock , /bin/cserver , /bin/main Please looking forward you guys help. (6 Replies)
Discussion started by: biswajitnitd
6 Replies

4. Shell Programming and Scripting

UNIX- Adding days to datetime variable

Hello All, I have a date with timestamp stored in a variable and another variable which stores no. of days. Var1=2014-12-11 12:54:21 Var2=5 In my unix script, I have to add the no. of days stored in var2 with the var1 date. Var3=var1+var2=2014-12-16 12:54:21 Can you please advise if... (8 Replies)
Discussion started by: Nikita1981
8 Replies

5. Shell Programming and Scripting

Add or Subtract the hours,minutes or seconds in the the time variable

Hello All, I am working on script where I need to add hours,minutes or seconds in the time.Time is not the current but it could be future time.I thought I can store that time in variable and add hours.minutes or second but I am not able to add that in the time that is stores in a variable. Time... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

6. Shell Programming and Scripting

How to add a datetime stamp at a particular position in a filename?

hi, i have some files in a directory say abc.txt def.txt ghi.txt i am storing these file names in a temp file. ls -l | grep "^-" | awk '{print $NF}' > temp_file$$ i want to add a date time stamp at a particular place in the file names. it can be 1) before the extension... (2 Replies)
Discussion started by: Little
2 Replies

7. Shell Programming and Scripting

[Solved] Working with date (add minutes using variables)

Dear all, today I'm scratching my head with a simple (I believe) issue. Working with date is quite simple, so if I Need to add some seconds to current time, I'll use: date --date='+30 seconds' +"%Y-%m-%d %H:%M:%S"But, how to pass the value to add from a variable? I tried the following without... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

8. Shell Programming and Scripting

python test datetime 30 minutes ago

Hello, RHEL5.5 PYTHON=2.4.3 I have 2 python variables using the datetime module. Here is how I call them: print "Current Time: %s" % now print "LastDownloadTime: %s" % LastDownloadTime Here is an example of an issue. Current Time: 2012-01-05 14:06:09.749240... (2 Replies)
Discussion started by: jaysunn
2 Replies

9. Shell Programming and Scripting

How to Increment or add minutes???;-)

Hi all, I want to add a minute to present time. E.g: if present time is 09:55, I want to make it 09:56. Please help!! I tried below script #!/bin/ksh timeut=`date -u '+%R'` let timeut1=$timeut + 1 echo "timeut1 = $timeut1" Regards Prashant:confused: (14 Replies)
Discussion started by: prashant43
14 Replies

10. Shell Programming and Scripting

Convert minutes to hours, minutes, seconds

How would you convert lets say a 1000 minutes to hours, minutes, seconds (1 Reply)
Discussion started by: Vozx
1 Replies

Featured Tech Videos