calculate date format in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculate date format in a loop
# 1  
Old 05-06-2009
calculate date format in a loop

I have to calculate the below in date format in the loop so tht it increases each day; can you please tell me how do I calculate the 1
v_my_DT = v_my_DT + 1.
also I need to stop the loop; I am using it in do while statement i posted earlier
It should be
20090506=20090505 + 1

Sorry v_my_dt is in string format
Thanks,
# 2  
Old 05-06-2009
???
Doesnt
Code:
let v_my_DT=$v_my_DT+1

work?
# 3  
Old 05-06-2009
I need this basically, u see the end date every month end..i am not getting it

20090501= 20090430 + 1

I am sorry to say but it' s really urgent...any clue .

Thanks
# 4  
Old 05-06-2009
so you need a script like:
Code:
#!/usr/bin/ksh

LASTDAY=`cal | sed '/^$/d' | tail -1 | head -1 | awk '{print $NF}'`
echo $LASTDAY

CURR_DATE=`date +%d`
echo $CURR_DATE

if [[ "${CURR_DATE}" -eq "${LASTDAY}" ]]; then
print "Today is the last day of the month"
#do your stuff
else
print "Today is NOT the last day of the month"
fi

to test if today is the last day of the month, then ... I let you imagine how to increment (easy but dont forget months 12 then year values...)
# 5  
Old 05-07-2009
Thanks vbe ...it's awesome >>>but that is not what I am looking at:

my requiremnt is this

I need to get the dates between the following two dates

DT1= 20090429 [is in string format]
DT2= 20090502 [is in string format]
can I change that to a date format? [I need this because of the end date problem ...i am getting 20090431]

Do calculation to increment the date by 1 day [2009/04/29,2009/04/30, 2009/05/01, 2009/05/02]

and again change the date format to string format in YYYYMMDD[20090429,20090430, 20090501, 20090502]
# 6  
Old 05-08-2009
Quote:
Originally Posted by aronmelon
...
I need to get the dates between the following two dates

DT1= 20090429 [is in string format]
DT2= 20090502 [is in string format]
can I change that to a date format?
...
Do calculation to increment the date by 1 day
...
and again change the date format to string format...
Here's one way to do it:

Code:
$ 
$ cat datediff.pl
#!/usr/bin/perl  
use Date::Calc;  

$date1 = $ARGV[0];
$date2 = $ARGV[1];

# @dt = (year,month,day)
@dt1 = unpack("A4 A2 A2",$date1);
@dt2 = unpack("A4 A2 A2",$date2);

$dd = Date::Calc::Delta_Days($dt1[0],$dt1[1],$dt1[2],$dt2[0],$dt2[1],$dt2[2]);
print "Diff in days = $dd\n";                                                 
if ($dd > 0){                                                                 
  @sd = @dt1;                                                                 
  @ed = @dt2;                                                                 
} elsif ($dd < 0) {                                                           
  @sd = @dt2;                                                                 
  @ed = @dt1;                                                                 
} else {                                                                      
  @sd = @dt1;                                                                 
  @ed = @dt1;                                                                 
}                                                                             

# print 'em
printf("%-14s%d/%02d/%02d","Start Date =>",$sd[0],$sd[1],$sd[2]);
foreach $i (1..abs($dd)) {                                       
  @x = Date::Calc::Add_Delta_Days($sd[0],$sd[1],$sd[2],$i);      
  printf("\n%10s%4s%d/%02d/%02d",$i," => ",$x[0],$x[1],$x[2]);   
}                                                                
print " <== End Date\n";

$
$ perl datediff.pl 20091225 20100105
Diff in days = 11
Start Date => 2009/12/25
         1 => 2009/12/26
         2 => 2009/12/27
         3 => 2009/12/28
         4 => 2009/12/29
         5 => 2009/12/30
         6 => 2009/12/31
         7 => 2010/01/01
         8 => 2010/01/02
         9 => 2010/01/03
        10 => 2010/01/04
        11 => 2010/01/05 <== End Date
$
$ perl datediff.pl 20100104 20091227
Diff in days = -8
Start Date => 2009/12/27
         1 => 2009/12/28
         2 => 2009/12/29
         3 => 2009/12/30
         4 => 2009/12/31
         5 => 2010/01/01
         6 => 2010/01/02
         7 => 2010/01/03
         8 => 2010/01/04 <== End Date
$
$ perl datediff.pl 20100104 20100104
Diff in days = 0
Start Date => 2010/01/04 <== End Date
$
$

Hope that helps,
tyler_durden

________________________________________________________
"This is your life and it's ending one minute at a time."
# 7  
Old 05-09-2009
if you have Python, here's an alternative
Code:
import datetime
mydate = "20090506"
year,mth,day = int(mydate[:4]),int(mydate[4:6]),int(mydate[6:])
diff = datetime.timedelta(days=1)
thedate = datetime.date(year,mth,day)
print thedate + diff

output
Code:
 # ./test.py
2009-05-07

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

3. Shell Programming and Scripting

Calculate Julian date of a given date

How to get Julian date (Three digit) of a given date (Not current date)? I do not have root privilege - so can not use date -d. Assume that we have three variables year, month and date. Thx (5 Replies)
Discussion started by: Soham
5 Replies

4. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

5. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

6. Shell Programming and Scripting

calculate time from a given format

Hi i have a file which consists of the time records in following format H:MM:SS.sss 0:00:09.249 0:00:00.102 0:00:00.105 0:00:08.499 0:00:08.499 0:00:06.980 0:00:04.249 0:00:05.749 0:00:00.108 0:00:00.107 0:00:03.014 0:00:00.000 I need to calculate their equivalent milliseconds... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

7. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

9. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

10. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies
Login or Register to Ask a Question