date substraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date substraction
# 1  
Old 02-25-2011
date substraction

hello
i have obtained the current date ..
current_date=date "+%m/%d%y"

and i have another date ,stored in my log file which i have already retrieved. i want to store the subtraction in a varible called diff.

diff=log_date - currentdate
ex: log_date=01/28/11
current_date= 02/25/11
then diff=28
# 2  
Old 02-25-2011
Hi, Try the below bash script ,
Date_Difference_Days.sh
Code:
#!/bin/sh
cur_date=`date "+%m/%d/%y"`
log_date='01/28/11'
echo $log_date $cur_date | awk '
BEGIN {
        d[1] = 31
        d[2] = 28
        d[3] = 31
        d[4] = 30
        d[5] = 31
        d[6] = 30
        d[7] = 31
        d[8] = 31
        d[9] = 30
        d[10] = 31
        d[11] = 30
        d[12] = 31
}
{
        yr=substr($1,7,2);mon=substr($1,1,2);day=substr($1,4,2)
        yr1=substr($2,7,2);mon1=substr($2,1,2);day1=substr($2,4,2)

        if((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0) { d["02"] = 29;}

        if ((yr1 - yr)==0 && (mon1-mon)==0) {print day1-day;exit}

        if ((yr1 - yr)==0 && (mon1-mon)!=0)
        {
        for (i=mon+1;i<int(mon1);i++){dayadd+=d[i]}
        print day1+(d[int(mon)]-day)+dayadd
        exit
        }

        if ((yr1 - yr)!=0)
        {
        mon_new=mon
        for(j=yr;j<yr1;j++)
        {
                for (i=mon_new+1;i<=12;i++){dayadd+=d[i]}
                mon_new=0
        }
        for (i=1;i<int(mon1);i++){dayadd+=d[i]}
        print "Days Diff :- " day1+(d[int(mon)]-day)+dayadd
        exit
        }
}'

# 3  
Old 02-25-2011
one more version , but this does have some limitations , like year<=2099 etc.

Code:
 
#!/bin/sh
get_JD () {
scale=0
echo " $1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4 " | bc
}
first=`get_JD 1 1 2011`
second=`get_JD 1 3 2011`
diff=`expr $second - $first`
echo $diff

I googled it on my intereset. The logic applied here seems to be an algo used in US Navy. I don't have much idea how this is working!!!
# 4  
Old 02-25-2011
Using gnu date
Code:
from="20100401" 
to="20100503" 
start=$(date --date="$from" +"%s") 
end=$(date --date="$to" +"%s") 
days=$(( (end - start) / 86400 )) 
echo $days 

from="04/01/11" 
to="05/03/11" 
start=$(date --date="$from" +"%s") 
end=$(date --date="$to" +"%s") 
days=$(( (end - start) / 86400 )) 
echo $days

Using ksh93 builtin printf
Code:
((day=24*60*60))  # 86400 s
fromdate="04/01/11"
todate="05/03/11"
# datestr => epoc 
epoc1=$(printf "%(%#)T" "$fromdate") 
epoc2=$(printf "%(%#)T" "$todate") 
# length of day (seconds) = 86400 
echo $(( (epoc2-epoc1) /day)) 

fromdate="2011-04-01"
todate="2011-05-03"
# datestr => epoc 
epoc1=$(printf "%(%#)T" "$fromdate") 
epoc2=$(printf "%(%#)T" "$todate") 
echo $(( (epoc2-epoc1) /day))

Or using calculating like panyam.

More about calculating
Is there a formula for calculating the Julian day number?

You can do panyam solution without any external commands, using builtin in every posix compatible shells (bash, ksh93, dash, ...)
Code:
get_JD()
{
        d="$1"
        m="$2"
        y="$3"
        echo $(( d-32075+1461*(y+4800+(m-14)/12)/4+367*(m-2-(m-14)/12*12)/12-3*((y+4900+(y-14)/12)/100)/4 ))
}

first=$( get_JD 1 4 2011 )       # day month year
second=$( get_JD 4 5 2011 )
diff=$((second - first))
echo $diff


Last edited by kshji; 02-26-2011 at 05:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
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

Substraction of matching lines from a file.

I have 2 files: file1.txt contains /html/mybook/Charts/143712/reptiles.pdf /html/mybook/Charts/198459/spices.pdf /html/mybook/Charts/198459/fresh_nuts.pdf /html/mybook/Charts/123457/dome_anim.pdf /html/mybook/Charts/123457/vegetables.pdf /html/content/3DInteractive/174091/CSPSGGB.html ... (6 Replies)
Discussion started by: Jojan Paul
6 Replies

4. Shell Programming and Scripting

Substraction in shell scripting

Hello friends, I am new on linux, i am facing issues on below script. #!/bin/sh current=1355147377 echo $current last_modified=1354537347 echo $last_modified DIFF='expr ($current - $last_modified)' echo $DIFF Please view this code tag video for how to use code tags when posting... (8 Replies)
Discussion started by: sanjay833i
8 Replies

5. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

6. Shell Programming and Scripting

Problem in algebraic substraction

my code is like this count=`cat /filecount.txt | tail -1 |head -1| awk '{print $1}'` ###file is having value 264 #### echo "actual count = $count" exact_count=`expr $value \* 24` echo "exact_count= $exact_count" diff=`expr "$exact_count" - "$count"` a= exact_count - count ... (8 Replies)
Discussion started by: sagar_1986
8 Replies

7. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. Solaris

Substraction in bash

Hi all, I have in one script something like this: FIRSTOCC=`grep -n ORA- alert_bill2.log |tail -"$ROWS"|head -1|cut -d: -f1` TOTAL=`more alert*|wc -l` DIFFERENCE=`$TOTAL-$FIRSTOCC` echo Total lines in alert_bill = $TOTAL echo $DIFFERENCE How do I make this substraction work? Thk (2 Replies)
Discussion started by: mclaudiu
2 Replies

9. UNIX for Dummies Questions & Answers

Date Substraction

In Unix script, how to get a "date - 1" ie, yesterday? (4 Replies)
Discussion started by: AC
4 Replies
Login or Register to Ask a Question