KEMBAR78
Python Programming Essentials - M23 - datetime module | PPTX
http://www.skillbrew.com
/SkillbrewTalent brewed by the
industry itself
Datetime module
Pavan Verma
@YinYangPavan
1
Founder, P3 InfoTech Solutions Pvt. Ltd.
Python Programming Essentials
© SkillBrew http://skillbrew.com
Introduction to datetime module
2
The datetime module supplies classes for
manipulating dates and times
Key classes in datetime module
Date Time Datetime Timedelta tzinfo
© SkillBrew http://skillbrew.com
Types of datetime objects
Date and Time
objects
Naive
objects
Aware
objects
3
Naive objects are not
aware of time zones and
day-light savings but they
are pretty easy to
understand
An aware object has
sufficient knowledge of
time zones and day-light
savings
© SkillBrew http://skillbrew.com
Note
4
All examples in these slides are using naive objects
© SkillBrew http://skillbrew.com
Date objects
5
from datetime import date
d = date(2013, 8, 22)
print(d.year)
print(d.month)
print(d.day)
print(d.strftime("%Y %m %d"))
Output:
2013
8
22
2013 08 22
© SkillBrew http://skillbrew.com
Date objects (2)
6
>>> from datetime import date
>>> date.today()
datetime.date(2013, 10, 21)
© SkillBrew http://skillbrew.com
Time objects
7
from datetime import time
t = time(2, 30, 45)
print(t.hour)
print(t.minute)
print(t.second)
print(t.strftime("%H:%M:%S"))
Output:
2
30
45
02:30:45
© SkillBrew http://skillbrew.com
DateTime objects
8
from datetime import datetime
dt = datetime(2013, 8, 21, 2, 30, 45)
print(dt.hour)
print(dt.minute)
print(dt.second)
print(dt.strftime("%m-%d-%Y %H:%M:%S"))
Output:
2
30
45
08-21-2013
02:30:45
© SkillBrew http://skillbrew.com
Manipulating dates
9
from datetime import datetime, timedelta
dt = datetime(2013, 8, 21, 2, 30, 45)
print(dt.strftime("%m-%d-%Y %H:%M:%S"))
Output:
08-21-2013 02:30:45
© SkillBrew http://skillbrew.com
Manipulating dates (2)
10
from datetime import datetime, timedelta
dt = datetime(2013, 8, 21, 2, 30, 45)
# get datetime after 5 days
add_dt = dt + timedelta(days=5)
print(add_dt.strftime("%m-%d-%Y %H:%M:%S"))
Output:
08-26-2013 02:30:45
© SkillBrew http://skillbrew.com
Manipulating dates (3)
11
from datetime import datetime, timedelta
dt = datetime(2013, 8, 21, 2, 30, 45)
# get datetime after 5 days
add_dt = dt + timedelta(days=5)
# get datetime 2 days in past
sub_dt = dt - timedelta(days=2)
print(sub_dt.strftime("%m-%d-%Y %H:%M:%S"))
Output:
08-19-2013 02:30:45
© SkillBrew http://skillbrew.com
Manipulating dates (4)
12
from datetime import datetime, timedelta
dt = datetime(2013, 8, 21, 2, 30, 45)
# get datetime after 5 days
add_dt = dt + timedelta(days=5)
# get datetime 2 days in past
sub_dt = dt - timedelta(days=2)
# comparison
print add_dt > sub_dt
Output:
True
© SkillBrew http://skillbrew.com
Manipulating dates (5)
13
from datetime import datetime, timedelta
dt = datetime(2013, 8, 21, 2, 30, 45)
# get datetime after 5 days
add_dt = dt + timedelta(days=5)
# get datetime 2 days in past
sub_dt = dt - timedelta(days=2)
# comparison
add_dt > sub_dt
# subtract
print add_dt - sub_dt
Output:
7 days, 0:00:00
© SkillBrew http://skillbrew.com
Summary
 Introduction to datetime module
 Types of datetime objects
 Date objects
 Time objects
 Datetime objects
 Manipulating Dates
14
© SkillBrew http://skillbrew.com
Resources
 Datetime objects blog post
http://buddylindsey.com/python-date-and-datetime-
objects-getting-to-know-them/
 http://docs.python.org/2/library/datetime.html
15

Python Programming Essentials - M23 - datetime module

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Datetime module Pavan Verma @YinYangPavan 1 Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.com Introductionto datetime module 2 The datetime module supplies classes for manipulating dates and times Key classes in datetime module Date Time Datetime Timedelta tzinfo
  • 3.
    © SkillBrew http://skillbrew.com Typesof datetime objects Date and Time objects Naive objects Aware objects 3 Naive objects are not aware of time zones and day-light savings but they are pretty easy to understand An aware object has sufficient knowledge of time zones and day-light savings
  • 4.
    © SkillBrew http://skillbrew.com Note 4 Allexamples in these slides are using naive objects
  • 5.
    © SkillBrew http://skillbrew.com Dateobjects 5 from datetime import date d = date(2013, 8, 22) print(d.year) print(d.month) print(d.day) print(d.strftime("%Y %m %d")) Output: 2013 8 22 2013 08 22
  • 6.
    © SkillBrew http://skillbrew.com Dateobjects (2) 6 >>> from datetime import date >>> date.today() datetime.date(2013, 10, 21)
  • 7.
    © SkillBrew http://skillbrew.com Timeobjects 7 from datetime import time t = time(2, 30, 45) print(t.hour) print(t.minute) print(t.second) print(t.strftime("%H:%M:%S")) Output: 2 30 45 02:30:45
  • 8.
    © SkillBrew http://skillbrew.com DateTimeobjects 8 from datetime import datetime dt = datetime(2013, 8, 21, 2, 30, 45) print(dt.hour) print(dt.minute) print(dt.second) print(dt.strftime("%m-%d-%Y %H:%M:%S")) Output: 2 30 45 08-21-2013 02:30:45
  • 9.
    © SkillBrew http://skillbrew.com Manipulatingdates 9 from datetime import datetime, timedelta dt = datetime(2013, 8, 21, 2, 30, 45) print(dt.strftime("%m-%d-%Y %H:%M:%S")) Output: 08-21-2013 02:30:45
  • 10.
    © SkillBrew http://skillbrew.com Manipulatingdates (2) 10 from datetime import datetime, timedelta dt = datetime(2013, 8, 21, 2, 30, 45) # get datetime after 5 days add_dt = dt + timedelta(days=5) print(add_dt.strftime("%m-%d-%Y %H:%M:%S")) Output: 08-26-2013 02:30:45
  • 11.
    © SkillBrew http://skillbrew.com Manipulatingdates (3) 11 from datetime import datetime, timedelta dt = datetime(2013, 8, 21, 2, 30, 45) # get datetime after 5 days add_dt = dt + timedelta(days=5) # get datetime 2 days in past sub_dt = dt - timedelta(days=2) print(sub_dt.strftime("%m-%d-%Y %H:%M:%S")) Output: 08-19-2013 02:30:45
  • 12.
    © SkillBrew http://skillbrew.com Manipulatingdates (4) 12 from datetime import datetime, timedelta dt = datetime(2013, 8, 21, 2, 30, 45) # get datetime after 5 days add_dt = dt + timedelta(days=5) # get datetime 2 days in past sub_dt = dt - timedelta(days=2) # comparison print add_dt > sub_dt Output: True
  • 13.
    © SkillBrew http://skillbrew.com Manipulatingdates (5) 13 from datetime import datetime, timedelta dt = datetime(2013, 8, 21, 2, 30, 45) # get datetime after 5 days add_dt = dt + timedelta(days=5) # get datetime 2 days in past sub_dt = dt - timedelta(days=2) # comparison add_dt > sub_dt # subtract print add_dt - sub_dt Output: 7 days, 0:00:00
  • 14.
    © SkillBrew http://skillbrew.com Summary Introduction to datetime module  Types of datetime objects  Date objects  Time objects  Datetime objects  Manipulating Dates 14
  • 15.
    © SkillBrew http://skillbrew.com Resources Datetime objects blog post http://buddylindsey.com/python-date-and-datetime- objects-getting-to-know-them/  http://docs.python.org/2/library/datetime.html 15