The datetime
module is a standard library module that supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.
The datetime
module has three types for storing a point in time:
date
for year, month, day of monthtime
for hours, minutes, seconds, microseconds, time zone infodatetime
combinesdate
andtime
. It has the methodsdate()
andtime()
to get the corresponding date and time objects, and there's a handycombine
function to combine date and time into a datetime.
datetime.strptime
is the main routine for parsing strings into datetimes. It can handle all sorts of formats, with the format determined by a format string you give it:
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
datetime.strftime
is the routine to format datetime objects to string.
import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')
# output: '02/23/2012'
Format options available in the datetime documentation and at strftime.org.
To summarize:
strptime
= "string parse time"strftime
= "string format time"