In hp-ux as there is no GNU date
available, i.e the -d
and -s
options of the date
command are not available. How do I find the difference between two given dates in days (as we do in Solaris or bash shell)?
Asked
Active
Viewed 2,457 times
-2

Adrian Frühwirth
- 42,970
- 10
- 60
- 71

vini
- 87
- 12
-
What format are the dates in? – Adrian Frühwirth Apr 26 '13 at 17:12
-
they are in the format dd/mm/yyyy – vini Apr 29 '13 at 05:46
-
And you want the difference in decades/months/hours/microseconds...? You need to be a little more specific about what you are trying to do. – Adrian Frühwirth Apr 29 '13 at 19:16
-
I want the difference in days. – vini May 08 '13 at 08:33
1 Answers
0
There are actually many solutions to be found via Google, but here is one that I just stumbled across and like for its brevity. You would use its ansi_dn
function on both dates and calculate the difference.
diff_dates.sh
:
#!/bin/ksh
. ./ansi_dn.sh
date_to="$1"
date_from="$2"
echo $((
$(ansi_dn \
$(echo "${date_to}" | cut -d/ -f3) \
$(echo "${date_to}" | cut -d/ -f2) \
$(echo "${date_to}" | cut -d/ -f1) \
)
- $(ansi_dn \
$(echo "${date_from}" | cut -d/ -f3) \
$(echo "${date_from}" | cut -d/ -f2) \
$(echo "${date_from}" | cut -d/ -f1) \
)
))
(If you have the years, months and days of both dates in separate variables already the cut
nightmare is not needed, obviously.)
Example:
$ ./diff_dates.sh 08/05/2013 01/01/1939
27156
Let's verify correctness with GNU date
:
$ echo $(( ( $(date -d 2013-05-08 +%s) - $(date -d 1939-01-01 +%s) + 43200 ) / 86400))
27156

Community
- 1
- 1

Adrian Frühwirth
- 42,970
- 10
- 60
- 71
-
-
@KVR_GROUPS: The function that I linked to in the first sentence, found in this answer: http://unix.stackexchange.com/a/42415/36514 – Adrian Frühwirth Jun 13 '13 at 10:47
-
-
Explain what exactly? You copy the function I linked to your script and call it like I showed in my answer. – Adrian Frühwirth Jul 01 '13 at 07:46
-
can u say what exactly its doing as i am not understanding those weird lines of the code – vini Jul 15 '13 at 09:40