LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-11-2010, 08:55 PM   #1
snowball0916
Member
 
Registered: Mar 2006
Location: China
Distribution: CentOS
Posts: 89

Rep: Reputation: 15
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
 
Old 12-11-2010, 09:16 PM   #2
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
This should get you started:
Code:
declare -i $day_of_the_year=$(date -d 'yyyy-mm-dd' +%j)
 
Old 12-11-2010, 09:33 PM   #3
snowball0916
Member
 
Registered: Mar 2006
Location: China
Distribution: CentOS
Posts: 89

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by barriehie View Post
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
 
Old 12-12-2010, 01:40 AM   #4
speck
Member
 
Registered: Nov 2001
Location: California
Distribution: Slackware 14.1
Posts: 271

Rep: Reputation: 54
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
 
Old 12-12-2010, 01:47 AM   #5
snowball0916
Member
 
Registered: Mar 2006
Location: China
Distribution: CentOS
Posts: 89

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by speck View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble adding days to a passed in date string ne00 Programming 5 04-08-2009 04:35 PM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 12:12 PM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 08:43 AM
Deleting files after x days from the latest date file sriramsreedhars Programming 8 03-03-2007 09:30 AM


All times are GMT -5. The time now is 02:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration