[SunOS] How to find the difference in date time?
Date 1 : 2017-01-02 17:24:33 Date 2 : 2017-01-03 22:55:02
Answer : 1770 minutes
[SunOS] How to find the difference in date time?
Date 1 : 2017-01-02 17:24:33 Date 2 : 2017-01-03 22:55:02
Answer : 1770 minutes
Neither Sun OS nor Solaris is Linux; the native date
command doesn't have the facilities of GNU date
, AFAIK. It may be obvious how you do it on Linux, but it isn't so obvious how you do it with POSIX date
which is approximately what Sun OS or Solaris supports.
You should find How do I get the date from two weeks ago using Solaris date
? on Unix & Linux useful. It suggests that if you're using a recent enough version of Solaris (implying Solaris 11, I think, and excluding earlier versions), then you can use gdate
to access the facilities of GNU date
. You can then convert the two given date/time values to 'seconds since the epoch', subtract them, and divide by 60 to get the number of minutes in the interval between the times.
For example:
date1="2017-01-02 17:24:33"
date2="2017-01-03 22:55:02"
secs1=$(gdate -d "$date1" +'%s')
secs2=$(gdate -d "$date2" +'%s')
echo $(( ($secs2 - $secs1) / 60 ))
The output from that is 1770
.
The $(( … ))
is POSIX shell arithmetic, also supported by Korn shell and Bash. I don't know whether the Solaris 11 /bin/sh
supports it or not. Historically, that shell was a Bourne shell without support for $( … )
let alone $(( … ))
.
If you don't have gdate
already, consult the cross-referenced question for more information about optional GNU software that can be installed on Solaris from the official Solaris repositories (see Solaris default install user tools).