I have two dates in below format. I have to calculate the difference between two days in Seconds.
currentDateTime="08/Sep/2022:05:26:13"
logDateTime="07/Sep/2022:04:58:30"
diff=$(echo "$(($(date -d "$currentDateTime" '%s')-$(date -d "$currentDateTime" '%s')))")
echo $diff
The output is empty. Can someone help me here
CodePudding user response:
You need to set the date in this format:
08 Sep 2022 05:26:13
You can perform something like this:
currentDate=$(echo $currentDateTime | awk -F':' '{print $1}' | awk -F'/' '{print $1,$2,$3}')
currentTime=$(echo $currentDateTime | awk -F':' '{printf "%s:%s:%s",$2,$3,$4}')
logDate=$(echo $logDateTime | awk -F':' '{print $1}' | awk -F'/' '{print $1,$2,$3}')
logTime=$(echo $logDateTime | awk -F':' '{printf "%s:%s:%s",$2,$3,$4}')
diff=$(echo "$(($(date -d "$(echo "$currentDate $currentTime")" '%s')-$(date -d "$(echo "$logDate $logTime")" '%s')))")
CodePudding user response:
$ logDateTime="07/Sep/2022:04:58:30"
$ logDateTime="${logDateTime//:*/ ${logDateTime#*:}}"
$ currentDateTime="08/Sep/2022:05:26:13"
$ currentDateTime="${currentDateTime//:*/ ${currentDateTime#*:}}"
$ let diff=($(date %s -d "${currentDateTime//// }")-$(date %s -d "${logDateTime//// }"))
$ echo $diff
88063