3

I have due_by as a local datetime object the following solutions isn't working for me!

due_by = float(due_by.strftime("%s"))
due_by = datetime.utcfromtimestamp(due_by)

Thanks in advance!

Eduardo Coltri
  • 506
  • 3
  • 8
Osama Arshad
  • 43
  • 1
  • 5
  • https://docs.python.org/3/library/datetime.html – go2nirvana Sep 16 '20 at 13:01
  • Does this answer your question? [How to convert local time string to UTC?](https://stackoverflow.com/questions/79797/how-to-convert-local-time-string-to-utc) – baduker Sep 16 '20 at 13:06

1 Answers1

2
from datetime import datetime
from datetime import timezone

# Datetime to timestamp
t = datetime.now()
due_by = t.timestamp()
# 1600261731.016313
print(due_by)

# Timestamp to datetime
due_by = datetime.fromtimestamp(due_by, tz=timezone.utc)
# 2020-09-16 13:08:51.016313+00:00
print(due_by)
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22