LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-05-2012, 10:15 PM   #1
doint
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Script shell - Date / Time


Hi,

ex: today is 06012012 ( 06/01/2012 )and now i want count how many day from 12082011 to 06122012

Please help me,

Thanks.
 
Old 01-05-2012, 11:14 PM   #2
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,305

Rep: Reputation: 1141Reputation: 1141Reputation: 1141Reputation: 1141Reputation: 1141Reputation: 1141Reputation: 1141Reputation: 1141Reputation: 1141
Hi,

this can be done with the help of the date command. Have a look at the man page (and info page too if you want more details), then have a go at writing the script. Post back here if you get stuck.

Cheers,

Evo2.
 
Old 01-06-2012, 01:40 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
To expand on the above, the basic procedure involves first converting both dates to epoch seconds. This makes it possible to simply subtract one date from the other to get the difference in seconds. After that it's just simple arithmetic to convert that into the number of days.

The gnu date command has a -d option for converting an arbitrary date into any supported format, so start there.


BTW, just as an aside, if you search the web you're likely to find ready-made solutions to this problem already available. You don't think you're the first person to ever need to calculate the difference between two dates, do you?
 
Old 04-11-2012, 10:15 AM   #4
hroptatyr
LQ Newbie
 
Registered: Apr 2012
Posts: 6

Rep: Reputation: Disabled
I've written a bunch of date arithmetic tools (dateutils) that would make this task trivial:

Code:
ddiff -i '%d%m%Y' 12082011 06012012
=>
  147
The -i '%d%m%Y' specifies the input format, the output can be controlled with -f but defaults to days in this case.
 
1 members found this post helpful.
Old 04-12-2012, 09:09 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
That's a neat package. I've been wanting something like it for a while. It's the kind of thing that should be in the core utilities, or at least included in the default distro repositories.

Thanks.
 
  


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
I am attempting write a shell script to compare date/time stamps in a directory iwitham Linux - Newbie 3 09-12-2011 08:20 AM
convertion of seconds to date and time in linux shell script naveenese Linux - Newbie 13 08-17-2010 07:14 AM
Using sed in a shell script to add date and time seefor Programming 3 02-25-2009 01:21 PM
Shell Script - Date/Time runnerpaul Programming 9 08-20-2008 07:45 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM


All times are GMT -5. The time now is 12:56 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