Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use:</p> <pre><code>date1="Sat Dec 28 03:22:19 2013" date2="Sun Dec 29 02:22:19 2013" date -d @$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) )) -u +'%H:%M:%S' </code></pre> <p>And the output is:</p> <pre><code>23:00:00 </code></pre> <p>However, for arbitrary differences over 24 hours, you have to start worrying about how the value for days will be represented. Negative values will also present problems.</p> <p>The problem with this is that if you drop the format string, the date presented is:</p> <pre><code>Thu Jan 1 23:00:00 UTC 1970 </code></pre> <p>So, while this works for the two given date/time values, it is not really a general solution.</p> <p>Consider adding <code>%j</code> for the day of year; it will work semi-sanely for up to 1 year's difference.</p> <p>Otherwise, capture the difference in a variable and present the results with a separate calculation.</p> <pre><code>date1="Sat Dec 28 03:22:19 2013" date2="Sun Dec 29 02:22:19 2013" delta=$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) )) if [[ $delta -lt 0 ]] then sign="-"; delta=${delta/^-/} else sign="+" fi ss=$(( $delta % 60 )) delta=$(( $delta / 60 )) mm=$(( $delta % 60 )) delta=$(( delta / 60 )) hh=$(( $delta % 24 )) dd=$(( $delta / 24 )) printf "$sign%d %.2d:%.2d:%.2d\n" $dd $hh $mm $ss </code></pre> <p>Output:</p> <pre><code>+0 23:00:00 </code></pre> <p>(Meaning: a positive difference of 0 days, 23 hours, 0 minutes, 0 seconds.) Splitting a number of days such as 1000 into years, months and days is fraught without any reference dates. Here, there are reference dates to help, but it would still be ... fun.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload