LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to get the days from a certain date? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-the-days-from-a-certain-date-849705/)

snowball0916 12-11-2010 08:55 PM

how to get the days from a certain date?
 
Hi, guys
Do you have any idea of calculating days' distance?

for instance,
Code:

( 2010-9-16 ) minus (2010-9-1) = 16 days
I have read the date manual, but I can't find any help from there.
Could you kindly help?
Thanks,
Milo

barriehie 12-11-2010 09:16 PM

This should get you started:
Code:

declare -i $day_of_the_year=$(date -d 'yyyy-mm-dd' +%j)

snowball0916 12-11-2010 09:33 PM

Quote:

Originally Posted by barriehie (Post 4188396)
This should get you started:
Code:

declare -i $day_of_the_year=$(date -d 'yyyy-mm-dd' +%j)

hi, barriehie
Sorry, May be I am missing something on this problem:
Your solution works for current year.

But, this calculation is not always in current year,
Code:

[root@www ~]# declare -i day_of_the_year=$(date -d '1970-01-01' +%j)
[root@www ~]# echo $day_of_the_year
1

any idea?

Thanks,
br,
milo

speck 12-12-2010 01:40 AM

If you only want to use shell script (not Perl), then the following should work. Just pass the two dates on the command line.

Code:

./script.sh 2010-12-9 2010-10-3
Code:

#!/bin/sh

SECS_PER_DAY=86400

DATE1=`date -d $1 +%s`
DATE2=`date -d $2 +%s`

DIFF=$((($DATE1-$DATE2)/$SECS_PER_DAY))

echo $DIFF


snowball0916 12-12-2010 01:47 AM

Quote:

Originally Posted by speck (Post 4188482)
If you only want to use shell script (not Perl), then the following should work. Just pass the two dates on the command line.

Code:

./script.sh 2010-12-9 2010-10-3
Code:

#!/bin/sh

SECS_PER_DAY=86400

DATE1=`date -d $1 +%s`
DATE2=`date -d $2 +%s`

DIFF=$((($DATE1-$DATE2)/$SECS_PER_DAY))

echo $DIFF


Thanks speck. It's so cool. :)


All times are GMT -5. The time now is 02:57 PM.