LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Script shell - Date / Time (http://www.linuxquestions.org/questions/programming-9/script-shell-date-time-922317/)

doint 01-05-2012 10:15 PM

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.

evo2 01-05-2012 11:14 PM

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.

David the H. 01-06-2012 01:40 AM

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?

hroptatyr 04-11-2012 10:15 AM

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.

David the H. 04-12-2012 09:09 AM

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.


All times are GMT -5. The time now is 10:30 AM.