LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 03:49 AM   #1
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,714

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
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, 04:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 07:11 AM   #3
Bertical
Member
 
Registered: Oct 2008
Location: Kingdom of Mercia
Distribution: Slackware
Posts: 83

Rep: Reputation: 14
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, 06:55 PM   #4
Bertical
Member
 
Registered: Oct 2008
Location: Kingdom of Mercia
Distribution: Slackware
Posts: 83

Rep: Reputation: 14
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, 03:20 AM   #5
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,714

Original Poster
Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
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, 03:52 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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



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 01:47 AM
[SOLVED] looping thru input files with different number of days. btacuso Programming 8 11-03-2010 11:54 PM
trouble adding days to a passed in date string ne00 Programming 5 04-08-2009 04: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 12:12 PM
Deleting files after x days from the latest date file sriramsreedhars Programming 8 03-03-2007 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:31 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration