user0
user0

Reputation: 683

Calculate Days between two dates in AIX using Bash

I am trying to find out the no of days b/w two dates. Below is the code I have which is working perfectly 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 am executing above script on AIX box, I am getting below error:

date: Not a recognized flag: d

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

date: Not a recognized flag: d

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

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

It's a PROD env and I don't have admin access to install any pack on it.

Upvotes: 4

Views: 1673

Answers (1)

Zombo
Zombo

Reputation: 1

This assumes that a month is 1/12 of a year, and that you use proper 4 digit years:

#!/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))
}

Upvotes: 1

Related Questions