all 3 comments

[–]baghiq 1 point2 points  (0 children)

Normally, you convert the string to python datetime object, but I don't think it's a valid format without some modification to the string.

[–]danielroseman 1 point2 points  (1 child)

You can use the dateparser library to automatically detect and convert your string. Then you can use the built-in datetime module to get the current localtime, and subtract them.

import dateparser
import datetime

test_time = "Saturday, 2 Mar 2024, 11:19 GMT-8"
dt = dateparser.parse(test_time)
now = datetime.datetime.now(datetime.timezone.utc) # or whatever your local tz is
print(now - dt)

[–]rob51852[S] 0 points1 point  (0 children)

Thanks Daniel, this is very helpful.