Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-07-2009, 09:18 PM   #1
LQ Newbie
Registered: May 2009
Posts: 24

Rep: Reputation: 0
Question 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?)

Old 05-08-2009, 09:22 AM   #2
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
In 'man date' :
-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.


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
Difference between two dates Uday123 Programming 5 02-23-2009 06:26 AM
Calculating seconds newbie_adm Programming 8 10-21-2008 12:29 PM
calculating throughput ilnli Programming 6 07-17-2007 01:58 AM
calculating in bash ovince Programming 3 03-28-2007 10:50 AM
calculating buffer size cynthia_thomas Programming 1 01-19-2006 10:49 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration