0
votes

I need to assert date time in my test, both of them are not in the same format. Date time in database is in (2021, 5, 10, 0, 0) and JSON returns date time in '2021-05-10T00:00:00'.

I'm struggling on how to convert one of the formats so it matches the other and then I can write assert. Any help would be greatly appreciated.

Thanks

2
can you not convert one to the other format using the format option? then you can check for both using the compare option. You can use the datetime option to get y, m, d, hh, mm values.Joe Ferndz
Try this x = str(datetime.datetime(2021, 5, 10, 0, 0))Joe Ferndz
You can parse the JSON string into a datetime with strptime and compare it with the datetime in database, see this answer.Lemayian
Thanks @JoeFerndzuser29496
i would use strptime. You need to be careful if JSON gives you seconds (other than 00) and the datetime from DB does not give. Or visa versa.Joe Ferndz

2 Answers

1
votes
d = datetime.datetime.strptime('2021-05-10T00:00:00', '%Y-%m-%dT%H:%M:%S')

You can do this to convert your datetime from JSON to a datetime object and then you can compare with the other one.

0
votes

Tried below and it worked. Both returns time in same format

y = str(datetime.datetime(2021, 5, 10, 0, 0))
print(y)

a = "2021-05-10T00:00:00"
print("aa", datetime.datetime.strptime(a, '%Y-%m-%dT%H:%M:%S'))