0% found this document useful (0 votes)
174 views

Python Datetime

The document discusses Python's datetime module which contains four objects - datetime, date, time, and timedelta. It provides examples of creating datetime, date, time objects and performing operations like addition and replacement. The datetime object has useful methods like now(), today(), weekday(), combine(), strftime(), and strptime(). The timedelta object is used for date arithmetic and can only be used with datetime and date, not time. The replace() method can be used to modify dates, times and datetimes.

Uploaded by

Superquant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Python Datetime

The document discusses Python's datetime module which contains four objects - datetime, date, time, and timedelta. It provides examples of creating datetime, date, time objects and performing operations like addition and replacement. The datetime object has useful methods like now(), today(), weekday(), combine(), strftime(), and strptime(). The timedelta object is used for date arithmetic and can only be used with datetime and date, not time. The replace() method can be used to modify dates, times and datetimes.

Uploaded by

Superquant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1. Python built- in package Datetime has four objects: datetime, date, time and timedelta.

import datetime as dt
date_time = dt.datetime(2015, 3, 21, 22, 0, 0) #2015-03-21 22:00:00
date = dt.date(2015, 3, 22) #2015-03-22
time = dt.time(21, 30, 00) #21:30:00
time_delta = dt.timedelta(days=1) #1 day, 0:00:00

2. Datetime object has some useful methods, such as now, today, weekday, combine, strftime and
strptime.
import datetime as dt
now = dt.datetime.now() #2015-03-20 14:56:07.404000
today = dt.datetime.today() #2015-03-20 14:56:07.404000
weekday = now.weekday() #4, Mon=0, Sun=6
strftime = today.strftime('%Y/%m/%d %H:%M:%S.%f')

date = dt.date(2015, 3, 22)


time = dt.time(21, 30, 00)
combine = dt.datetime.combine(date, time) #2015-03-22 21:30:00

string = '2015-03-22 10:34:21.123'


format = '%Y-%m-%d %H:%M:%S.%f'
strptime = dt.datetime.strptime(string, format)

3. Timedelta is a useful object for date mathematics.


import datetime as dt
date_time = dt.datetime(2015, 3, 21, 22, 0, 0)
date = dt.date(2015, 3, 22)
time_delta = dt.timedelta(days=1, seconds=10)
date_time + time_delta #2015-03-22 22:00:10 type: datetime.datetime
date + time_delta #2015-03-22 type: datetime.date

#Note: timedelta object is only for datetime and date objects, not
for time object.

4. Use replace method to modify dates, times and datetimes.


import datetime as dt
date_time = dt.datetime(2015, 3, 21, 22, 0, 0)
replace = date_time.replace(year=2012, month=9) #2012-09-21 22:00:00

Author: Peng Xu

References:
https://docs.python.org/2/library/datetime.html
Sheppard, Kevin. "Introduction to Python for econometrics, statistics and data analysis.", University
of Oxford, version 2 (2014).

You might also like