LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-13-2010, 04:49 AM   #1
LinusStallman
Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Linux Mint 17 MATE
Posts: 285

Rep: Reputation: 14
Lightbulb Offline method required to find out number of days since a certain date!


Hi.

What offline method is there of finding out days since a certain date. Example: How would someone find the number of days from 1-Jan-2003 to 7-Dec-2010? Could someone write a script that takes in the 2 dates and output the number of days?

Thanks.
 
Old 12-13-2010, 05:49 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
What have you tried so far? Basically you need to convert the two dates in seconds since the system reference time (epoch for unix shells), subtract them and divide the result by 86400. If you're interested in the fractional part you can use either bc or awk, since the shell doesn't do floating point arithmetic.
 
Old 12-13-2010, 08:11 AM   #3
Bertical
Member
 
Registered: Oct 2008
Distribution: Slackware
Posts: 65

Rep: Reputation: 5
Code:
[cpt_scarlet@spectrum:~] $ ((datediff=(( `date -u -d "2010-07-12" +%s` - `date -u -d "2003-01-01" +%s`))))
[cpt_scarlet@spectrum:~] $ echo $((datediff / 86400 ))
[cpt_scarlet@spectrum:~] $ 2749
 
Old 12-13-2010, 07:55 PM   #4
Bertical
Member
 
Registered: Oct 2008
Distribution: Slackware
Posts: 65

Rep: Reputation: 5
Or it might be

Code:
[cpt_scarlet@spectrum:~] $ ((datediff=(( `date -u -d "2010-12-07" +%s` - `date -u -d "2003-01-01" +%s`))))
[cpt_scarlet@spectrum:~] $ echo $((datediff / 86400 ))
[cpt_scarlet@spectrum:~] $ 2897
please check before handing in homework.
 
Old 12-14-2010, 04:20 AM   #5
LinusStallman
Member
 
Registered: Mar 2010
Location: Ireland
Distribution: Linux Mint 17 MATE
Posts: 285

Original Poster
Rep: Reputation: 14
Exclamation

Quote:
Originally Posted by Bertical View Post
Or it might be

Code:
[cpt_scarlet@spectrum:~] $ ((datediff=(( `date -u -d "2010-12-07" +%s` - `date -u -d "2003-01-01" +%s`))))
[cpt_scarlet@spectrum:~] $ echo $((datediff / 86400 ))
[cpt_scarlet@spectrum:~] $ 2897
please check before handing in homework.
Hi Bertical. No, it is not homework. I am just curious as to how to calculate it. I don't understand how to run your solution though.
 
Old 12-14-2010, 04:52 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can either write a script or a function passing the two dates as arguments. Example, if you define a function:
Code:
function datediff () {
  echo $(( ($(date -ud "$2" +%s) - $(date -ud "$1" +%s)) / 86400 ))
}
then you can simply launch a command like this:
Code:
$ datediff 1-Jan-2003 7-Dec-2010
2897
The two dates must be in a standard format recognized by the date command. See info date for details.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to get the days from a certain date? snowball0916 Linux - Newbie 4 12-12-2010 02:47 AM
[SOLVED] looping thru input files with different number of days. btacuso Programming 8 11-04-2010 12:54 AM
trouble adding days to a passed in date string ne00 Programming 5 04-08-2009 05:35 PM
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 01:12 PM
Deleting files after x days from the latest date file sriramsreedhars Programming 8 03-03-2007 10:30 AM


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

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