Skip to content

the-right-way-of/using-datetime-in-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

using-datetime-in-python

Stackoverflow is just full of nonesense, so its better to create this one based on my experience This is based on python3 by the way.

Datetime with timezone?

Python's datetime library is quite good when dealing with date and time in general. Its not comparable to the time library however because that is supposed to be unix or windows process time operations. Using datetime is relatively straightforward:

#!/usr/bin/env python3

import datetime
print(datetime.datetime.now()) # Outputs non-tz aware datetime but local clock based
print(datetime.datetime.now().astimezone()) # Outputs tz aware datetime in local clock and local timezone
print(datetime.datetime.now(datetime.timezone.utc)) # Converts tz aware datetime in UTC clock time and assign UTC timezone

dt = datetime.datetime.now()
dt = dt.replace(tzinfo=datetime.timezone.utc) # Replace (not convert) the timezone of line 13 with UTC (mucks up the time)
print(dt)
print(dt - datetime.datetime.now(datetime.timezone.utc)) # Gives you the difference between UTC and Your time zone ;)

dt - datetime.datetime.now() # Throws an error saying "TypeError: can't subtract offset-naive and offset-aware datetimes"

About

Stackoverflow is just full of nonesense, so its better to create this one based on my experience

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published