LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   How do I calculate the number of days from a particular date using the <cal> utility? (https://www.linuxquestions.org/questions/linux-desktop-74/how-do-i-calculate-the-number-of-days-from-a-particular-date-using-the-cal-utility-610070/)

deepumnit 12-31-2007 01:30 AM

How do I calculate the number of days from a particular date using the <cal> utility?
 
How do I do it? I want to calculate. Is there any other method?

paulsm4 12-31-2007 02:26 AM

Hi -

I don't think you can (easily) do it with "cal".

You *can*, however, do it with "date".

Specifically, with "date '+%j'"

The trick is:
1. Convert your "from" date to Julian format
2. Simply add/subtract the days you want
3. Convert back from Julian to normal MM/DD/YYYY (or whatever) format

'Hope that helps .. PSM

ghostdog74 12-31-2007 03:16 AM

GNU date
Code:

# date +%Y%m%d -d "1 day ago"
20071230

info date for more examples

paulsm4 12-31-2007 12:12 PM

Good point, ghostdog74 - thank you!

Deepumnit - in general, "date diff" problems usually involve "Julian dates" as part of the solution. But there are very often "convenience functions" that give you a simpler, more direct solution. Like the one Ghostdog74 suggested :-)

Anyway - I presume your question is completely answered.

Happy New Year .. PSM


All times are GMT -5. The time now is 07:04 AM.