1

How to calculate the difference in seconds between below two date variables using Ansible.

date1: 06/01/2021 00:10:15.434 date2: 06/01/2021 00:10:26.667

Tried to_datetime gives does not match format '%Y-%m-%d %H:%M:%S' error. If the dates are in yyyy-mm-dd format the difference calculation works, but not getting with above date format.

Sumith
  • 41
  • 7
  • Does this help? https://stackoverflow.com/questions/48101921/ansible-compare-difference-between-two-dates-for-the-last-hour – Will Walsh Jun 01 '21 at 05:01

1 Answers1

1

Assuming 06/01/2021 is in the month, date, year format, use this code.

((date2 | to_datetime('%m/%d/%Y %H:%M:%S.%f')) - (date1 | to_datetime('%m/%d/%Y %H:%M:%S.%f')).total_seconds()
Will Walsh
  • 1,799
  • 1
  • 6
  • 15