Calculate days between two dates on AIX using Bash

I’m trying to find out if there are two days. Below is code that works great on UNIX.

date1=$(date "+%m/%d/%y")
temp1=4/8/24
echo $((($(date -u -d $temp1 +%s) - $(date -u -d $date1 +%s)) / 86400))

When I execute on a script in an AIX field, I get below the error:

date: unrecognized flag: d

Usage: date [-u] [+ "Field Descriptors"]

date: unrecognized flag: d

Usage: date [-u] [+ "Field Descriptors"]

(-) / 86400: syntax error: expected operand (error token ") / 86400") `

This is PROD env, and I do not have administrator access to install any packages on it.

+4
source share
1 answer

This assumes that the month is 1/12 of the year, and that you are using the correct 4-digit year:

#!/usr/bin/awk -f
function mktm(datespec) {
  split(datespec, q, "/")
  return \
  (q[3] - 1970) * 60 * 60 * 24 * 365.25 + \
  (q[1] -    1) * 60 * 60 * 24 * 365.25 / 12 + \
  (q[2] -    1) * 60 * 60 * 24
}
function ceil(x) {
  y = int(x); return y < x ? y + 1 : y
}
BEGIN {
  srand()
  print ceil((mktm(ARGV[1]) - srand()) / (60 * 60 * 24))
}
+1
source

Source: https://habr.com/ru/post/1666412/


All Articles