LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calculating the difference between two dates (https://www.linuxquestions.org/questions/linux-newbie-8/calculating-the-difference-between-two-dates-724548/)

mikejreading 05-07-2009 09:18 PM

Calculating the difference between two dates
 
Sorry guys,

Its late, and my brain isnt working.

Im trying to calculate the difference between two dates.

Doing this by taking Todays date is subtracting it from the Target date.

Im getting todays date with date +%s to get time since the epoch, but how do i convert the other time to it?

(there are a few examples, one of which is time since the last boot)

Using the command who -b how do i convert into UNIX time? (Or is there a better way of doing it?)

Cheers

berbae 05-08-2009 09:22 AM

In 'man date' :
Quote:

-d, --date=STRING
display time described by STRING, not `now'
So to convert the date from the 'who -b' command, you can do something like :

date -d "$(who -b|tr -s " "|cut -d " " -f 4-)" +%s

The 'tr' command eliminates the multiple spaces at the start of the output string, and the 'cut' command extracts only the date and time.
$(...) gives the output of the command inside it.


All times are GMT -5. The time now is 09:48 PM.