Increment date variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment date variable
# 1  
Old 08-02-2014
Increment date variable

hey guys,

I need to incerement the date variable for instance
Code:
echo `date '+%F %H:%M:00'`

this produces
Code:
2014-08-02 20:05:00

-I will grant this to : $Datehour and need to assign 1 hr from now to $Datelasthour
-the script time will be used to talk to DB system information.

however I need a second variable that is the exact same date + 1 hour

I need this for a script that will run between 9 am and 8 pm against a DB the time will be associated with the scheduling software and depends on business time thus I must use variables that pick system variable date and assign to a variable.
will update if my googing results in a answer, I feel like this is simple but yea any help would be awesome.
# 2  
Old 08-02-2014
What Linux/UNIX are you running?
# 3  
Old 08-02-2014
Rhel 5

---------- Post updated at 10:58 PM ---------- Previous update was at 10:48 PM ----------

Rhel 5
# 4  
Old 08-03-2014
Some people may suggest calling date twice, but if you do that just before and just after a change in the minute, the two values won't be related the way you want them. (As a close to worst case, that could give you values two hours and 1 minute apart instead of one hour apart.) Try something like:
Code:
#!/bin/ksh
read d h m <<-EOF
    $(date '+%F %H %M')
EOF
now="$d $h:$m:00"
next="$d $(printf '%02d:%02d:00' $((${h#0} + 1)) $m)"

This was tested using the Korn shell, but will work with any shell that performs POSIX standard shell command substitutions and parameter expansions (including bash and ksh, among others). To see that it works, you'll need to either trace what it is doing or print the results.

It won't work after 11pm, but that doesn't seem to be a problem for you requirements.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-03-2014
Hey Don, I found this online. However I prefer yours :S
Code:
DATE=2013-05-25
  for i in {0..8} do  
  NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
    echo $NEXT_DATE done


Results:

05-25-2013
05-26-2013
05-27-2013
05-28-2013
05-29-2013
05-30-2013
05-31-2013
06-01-2013
06-02-2013

Don Thanks your script does the trick exactly as I need Smilie

Last edited by mo_VERTICASQL; 08-03-2014 at 04:28 AM.. Reason: Thanks DON!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Urgent for date increment

hi all, i would like to increment the date variable i am using for((i=20190731;i<=20190801;i++)) do done after 20190731 it should be 20190801 but this taking as 20190732,20190733.... kindly help me to solve this (3 Replies)
Discussion started by: prathaban
3 Replies

2. UNIX for Beginners Questions & Answers

Script to find a date variable and increment it

Hi, I have parameter file wo_location.prm which has a date variable $last_upd_date= 02032016. I need to write a unix shell script to find that variable and increment it by 1 day. The path to the file is root/dir_lc/shared/param/wo_location.prm and the variable is $last_upd_date. Any... (2 Replies)
Discussion started by: isenhiem
2 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Date increment logic

Hi all, I need to increment date at run time. Example: I need to write a shell script with two parameters. 1. country code like (US,UK, IND.....) 2. Date range from_date to to_date (20070101 to 20070331) I need to run shell script like this country_info.sh US 20070101 20070331 ... (3 Replies)
Discussion started by: pmreddy
3 Replies

5. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

6. Shell Programming and Scripting

Date increment

hi Friends, Today_Dt=`date "+%Y-%m-%d"` So the Today date is 2010-05-03 I have a file which has date values as below 2010-04-27 2010-04-02 2010-04-18 2010-04-28 2010-04-29 .. (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

7. Shell Programming and Scripting

Increment in date

Hi, I have a variable lets say DATA_DATE. I have to pass some value to this variable in YYYYMMDD format. lets say today I have passed this variable as : DATA_DATE=20100107 Then pls help me how to calculate another variable DATA_DATE1 (which is DATA_DATE+1). The code should work... (3 Replies)
Discussion started by: 46019
3 Replies

8. Shell Programming and Scripting

Increment a date variable in perl script

Hi, I have a perl script which prints epoch value of date in milliseconds as the output. My reuirement is that once the output is printed,the day variable shld increment by 1 and when i execute the script for the second time the output shld be for the new day value. My script looks as... (11 Replies)
Discussion started by: jyothi_wipro
11 Replies

9. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

10. Shell Programming and Scripting

Can we increment or decrement a date value?

export a=`date` a=`expr $a + 1` Is it possible? if not how can i increment or decrement a date variable? (2 Replies)
Discussion started by: arghya_owen
2 Replies
Login or Register to Ask a Question