Published on

100 Days of Python - datetime module #Day 1

Authors

Overview

The datetime module provides classes for manipulating dates and times. In this article we will see how to use most common functions from datetime class of datetime module.

1. Get UTC datetime in Python

from datetime import datetime
dt = datetime.utcnow()
print(dt)
2022-06-22 15:56:27.594630

2. Convert datetime object to POSIX TimeStamp in Python

from datetime import datetime
dt = datetime.utcnow()
print(dt.timestamp())
1655893682.76576

3. Convert POSIX timestamp to datetime object in Python

from datetime import datetime
ts = 1655892233.487077
dt = datetime.fromtimestamp(ts)
print(dt)
2022-06-22 15:33:53.487077

4. Get current datetime in Python

from datetime import datetime
dt = datetime.now()
print(dt)
2022-06-22 21:28:59.812137

5. Get today's date without time in Python

from datetime import datetime
dt = datetime.now()
d = datetime.date(dt)
print(d)
2022-06-22

6. Convert datetime to UTC timezone in Python

from datetime import datetime, timezone
dt = datetime.now()
dt = dt.astimezone(tz=timezone.utc)
print(dt)
2022-06-22 15:59:31.792806+00:00

7. Get Weekday from datetime object in Python

from datetime import datetime
dt = datetime.now()
weekday = dt.weekday() # Monday = 0, Sunday = 6
print(weekday)
weekday = dt.isoweekday() # Monday = 1, Sunday = 7
print(weekday)
2
3

8. Convert datetime object to string in Python

from datetime import datetime
dt = datetime.now()
dt_str = dt.strftime(format="%Y-%b-%d:%H:%M:%S")
print(type(dt_str))
print(dt_str)

9. Convert string to datetime object in Python

from datetime import datetime
dt = datetime.strptime("2022-06-22 08:30:30", "%Y-%m-%d %H:%M:%S")
print(type(dt))
print(dt)
<class 'str'>
2022-Jun-22:21:30:18

10. Get year, month, day from datetime object in Python

from datetime import datetime
dt = datetime.now()
year, month, day = dt.year, dt.month, dt.day
print(year, month, day)
2022 6 22

11. Convert datetime to ISO format in Python

from datetime import datetime
dt = datetime.now()
dt_iso = dt.isoformat(timespec="seconds")
print(dt_iso)
dt_iso = dt.isoformat(timespec='microseconds')
print(dt_iso)
2022-06-22T21:31:23
2022-06-22T21:31:23.668440

12. Get all values with timetuple in Python

from datetime import datetime
dt = datetime.now()
for tt in dt.timetuple():
    print(tt)
2022
6
22
21
31
45
2
173
-1

13. Get the local datetime in Python

from datetime import datetime
dt = datetime.today()
print(dt)
2022-06-22 21:32:03.026102

14. Replace year, month, day etc.. in datetime object

from datetime import datetime
dt = datetime.now()
new_dt = dt.replace(year=2021, month=8, day=13)
print(new_dt)
2021-08-13 21:32:23.949571

15. Combine date and time object to create new datetime object

from datetime import datetime, date, time
d = date(2022, 10, 8)
print(type(d))
t = time(10, 34)
print(type(t))
dt = datetime.combine(d, t)
print(type(dt))
print(dt)
<class 'datetime.date'>
<class 'datetime.time'>
<class 'datetime.datetime'>
2022-10-08 10:34:00