Welcome back, guys!
In this chapter, we are going to learn DateTime in python.
Course content
- Introduction
- datetime.date
- datetime.time
- datetime.datetime
- datetime.timedelta
- Python format datetime
- Handling timezone in python
Classes in Datetime Module
- date class
- time class
- datetime Class
- timedelta Class
Introduction
- Python has a module named datetime to work with dates and times. Following are simple programs to understand date and time.
- To check what are the methods available in the datetime module.
import datetime as dt
datetime_object=dt.datetime.now()
print(datetime_object)
print(datetime_object)
Output=2021-08-29 18:32:59.048204
datetime.datetime(2021, 8, 29, 18, 32, 59, 48204)
date_object=dt.date.today()
print(date_object)
date_object
Output=2021-08-29
datetime.date(2021, 8, 29)
help(dt.datetime)
Output=Help on class datetime in module datetime:
class datetime(date)
| datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
|
| The year, month and day arguments are required. tzinfo may be None, or an
| instance of a tzinfo subclass. The remaining arguments may be ints.
|
| Method resolution order:
| datetime
| date
| builtins.object
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value........
datetime. date class
- We can instantiate date objects from the date class. A date object represents a date( year, month, and day)
Get date from timestamp
Here dt=datetime so don't get confused.
d=dt.date(2019,4,13)
print(d)
Output=2019-04-13
t=dt.date.today()
print(t)
Output=2021-08-29
t.year
Output=2021
t.day
Output=29
t.month
Output=8
timestamp = dt.date.fromtimestamp(1326244364)
print(timestamp)
Output=2012-01-11
- A Unix timestamp is the number of seconds between a particular date and January 1,1970 at UTC.
datetime. time class
- A time object instantiated from the time class represents the local time.
- Once we create a time object we can easily print its attributes such as hour, minute, etc.
a=dt.time()
print(a)
Output=00:00:00
b=dt.time(11,22,33)
print(b)
Output=11:22:33
c=dt.time(hour=12,minute=32,second=3)
print(c)
Output=12:32:03
d=dt.time(11,52,30,54568)
print(d)
Output=11:52:30.054568
a=dt.time(11,22,33)
print(a)
Output=11:22:33
a.hour
Output=11
a.minute
Output=22
a.second
Output=33
a.microsecond
Output=0
datetime.datetime class
- The datetime module has a class named datetime that can contain information from both date and time objects.
- Print year, month, hour, minute, and timestamp.
a=dt.datetime(2018,12,4)
print(a)
Output=2018-12-04 00:00:00
(year,month,day,hour,minute,second,microsecond)
b=dt.datetime(2018,1,3,11,23,44,30564)
print(b)
Output=2018-01-03 11:23:44.030564
a=dt.datetime(2019,12,13,11,55,12,23457)
print(a)
Output=2019-12-13 11:55:12.023457
a.hour
Output=11
a.year
Output=2019
a.day
Output=13
a.month
Output=12
a.timestamp()
Output=1576218312.023457
datetime.timedelta class
- A timedelta object represents the difference between two dates or datetimes.
- See the difference between two timedelta objects, printing negative timedelta object.
t1=dt.date(year=2018,month=7,day=12)
t2=dt.date(year=2017,month=12,day=23)
t3=t1-t2
print(t3)
Output=201 days, 0:00:00
t4=dt.datetime(year=2018,month=12,day=12,hour=7,minute=23,second=16)
t5=dt.datetime(year=2017,month=12,day=2,hour=9,minute=53,second=16)
t6=t4-t5
print(t6)
Output=374 days, 21:30:00
t1=dt.timedelta(weeks=2,days=5,hours=1,seconds=33)
t2=dt.timedelta(weeks=5,days=4,hours=2,seconds=13)
t3=t1-t2
print(t3)
Output=-21 days, 23:00:20
Python format datetime
- The way date and time are represented may be different in different places, organizations, etc.
- It's more common to use mm/dd/yyyy in the US, whereas dd/mm/yyyy is more common in the UK.
- Python has strftime() and strptime() methods to handle this.
Python strftime() - datetime object to string.
- The strftime() method is defined under classes date,datetime, and time.
- The method creates a formatted string from a given date, datetime, or time object.
n=dt.datetime.now()
print(n)
Output=2021-08-29 20:03:20.546111
t=n.strftime("%H:%M:%S")
print(t)
Output=20:03:20
s1=n.strftime("%m/%d/%Y,%H:%M:%S")
print(s1)
Output=08/29/2021,20:03:20
Python strptime() - string to datetime.
- The strptime() method creates a datetime object from a given string (representing date and time.)
date_string="21 June 2018"
print(date_string)
Output=21 June 2018
date=dt.datetime.strptime(date_string,"%d %B,%Y")
print(date)
Output=2018-06-21 00:00:00
Handling timezone in python
- Suppose, we are working on a project that needs to display date and time based on their timezone. Rather than trying to handle timezones ourselves, we use a third-party pytz module.
import pytz
d=dt.datetime.now()
print(d)
Output=2021-08-29 20:10:18.354722
d.strftime("%m/%d/Y,%H:%M:%S")
Output='08/29/Y,20:10:18'
tz_NY=pytz.timezone('America/New_York')
datetime_NY=dt.datetime.now(tz_NY)
print(datetime_NY)
Output=2021-08-29 10:40:19.502045-04:00
pytz.all_timezones
Output=['Africa/Abidjan',
'Africa/Accra',
'Africa/Addis_Ababa',
'Africa/Algiers',
'Africa/Asmara',
'Africa/Asmera',
'Africa/Bamako',
'Africa/Bangui',
'Africa/Banjul',
'Africa/Bissau',
'Africa/Blantyre',
'Africa/Brazzaville',
'Africa/Bujumbura',
'Africa/Cairo',
'Africa/Casablanca',
'Africa/Ceuta',
'Africa/Conakry',
'Africa/Dakar',
'Africa/Dar_es_Salaam',
'Africa/Djibouti',
'Africa/Douala'......etc.....
This is a small concept and easy to understand, so I hope that you have understood this topic.
So we will meet in the next chapter......
See you soon
Best regards from,
msbtenote :)
Comments
Post a Comment
If you have any query, please let us know