[Tutor] Through a glass, darkly: the datetime module

Brian van den Broek brian.van.den.broek at gmail.com
Sun Oct 7 05:50:10 CEST 2012

On 6 Oct 2012 22:40, "Richard D. Moores" <rdmoores at gmail.com> wrote:


> I remain bewildered. Where did these strangely named things come from,
> strftime and strptime? I see that

Hi Dick,

These names carry over from well entrentched names from C. My guess is
format time and print time are what they are supposed to suggest.

> >>> from datetime import date
> >>> date(2014, 2, 18).strftime("%A")
> 'Tuesday'
> gives me what I was after, but I need to understand it, and I
> understand very little of that section, "8.1.8. strftime() and
> strptime() Behavior".
> Take the first sentence in that section:
> "date, datetime, and time objects all support a strftime(format)
> method, to create a string representing the time under the control of
> an explicit format string. Broadly speaking, d.strftime(fmt) acts like
> the time module’s time.strftime(fmt, d.timetuple()) although not all
> objects support a timetuple() method."
> Total gibberish. I feel like I've hit a brick wall. Where can I go to
> learn to understand it? I need some very basic, specific information.

I expect your speaking from a place of frustration. I don't think this is
the high point of the docs, but it isn't so bad as that. The strftime
method of a datetime object uses a mechanism similar to string formatting
to create strings displaying data from the datetime object. The docs for
datetime don't provide the details, pointing instead to the docs for the
very similar method of the time module.

I'm on my phone so cannot easily show an example but if d is a
datetime.datetime instance, d.strftime('%Y') will produce a string of d's

Consult the time docs and see if you can get somewhere. If not, post again.


Brian vdB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121006/a7267d56/attachment.html>

More information about the Tutor mailing list