Issue
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
Solution
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')))")
Answered By - Abhishek S Answer Checked By - Marilyn (WPSolving Volunteer)