What are some common uses for the datetime library?


What are some common uses for the datetime library?


If you’re a Pro member, you have access to a project coming up in lesson 8 where you make a calendar! There are tons of features you could add to the basic calendar project that use the datetime library, like adding an alert when the date of an event equals the current date, displaying how many days away an event is, all kinds of things!
Aside from that, the uses for it are bounded only by your creativity. Lots of applications need to perform actions at a certain time daily, weekly, annually, whatever the case may be. Using datetime to detect when to do something is a powerful tool!


Years ago (around 1970) I worked with a 4GL (generation language) (NOMAD) that had clever date stuff and you could subtract dates and get how long between them – I will be curious to see if this is going to be available here as we progress.


I am getting different time from my PC time in the console window. what might be causing this problem?

Time in console

2018-12-18 13:27:34.689719

Time on my machine

2018, 12, 18, 6, 27, 33, 457710

Since we are UTC -7:00 that would indicate the console is tuned to UTC.

1 Like

Thank you for the reply.
Based on your reply I tried in my PC python console and the local time is being printed.
Just to know if I want to see my local time in Codecademy console, is there a way?
I tried to add some constant (difference time) for “now.hour” but it doesn’t work.

1 Like

When I try to use the same coding
print ‘%02d/%02d/%04d %02d:%02d:%02d’ % (now.month, now.day, now.year, now.hour, now.minute, now.second)
on my PC where I have Python 3.7.2, I end up getting errors whatever I do, any ideas what should be changed?

You are using Python 3, but are using Python 2 print formatting

(Main problem that is causing error message): Parentheses are required in Python 3 for the print() function

(Style point): It’s probably better to use string.format() method:

print('{:02}/{:02}/{:4}, {:02}:{:02}:{:02}'.format (now.month, now.day, now.year, now.hour, now.minute, now.second))

(Another style point): It’s probably not best to use a method name (now()) as a variable name. That’s the sort of thing that might work 99% of the time, but then lead to a difficult-to-trace error.


The member is following the instructions given in the exercise. There is nothing wrong with using the modulo string format method, just so long as the entire expression is in parens following print.

If one has not completed this track (string formating) it is difficult to know what is instruction, and what is user choice.

Bottom line… print() is a function in Python 3, so parens are mandatory. Python 3.7 supports all three formating methods…


I though that the “Style point” label made that clear. My apologies if it was not.

As shown in the video, the datetime module has its own formatting rubric that uses the % marker. The poster, however, was not using datetime formatting, but rather Python string formatting. Granted that % - style formatting is not under a threat of deprecation, the docs (by my reading - and I admit the language is a bit squishy) seem to be nudging users towards str.format() or f-strings.

1 Like

That would be the strptime and strftime methods?


1 Like

As far as Style Points go, the beginner has enough on their plate with learning syntax, concepts and keywords that we need not burden them with style points. Let them learn as they go and in time they will pick up on it without any harping from us.

I wonder. Is there a line?

When you see indexing unnecessarily used to iterate through a flat list, or an expression used to iterate through a dictionary that first reduces it to a list, do you suggest methods that might be more “Pythonic”?

How about

if x == True:
    return True
    return False

… or

if x:
    if y:
        if z:
            return True
return False

Would you point out that these constructions could be better formulated?

From a strictly beginner standpoint of view, return True and return False may help the learner to grasp the logic and where the two boolean states fit in.

But then the bar is raised and we are given to tackle truthy and falsy which are not boolean literals, but expressions that evaluate to True or False. Once we are comfortable with this concept, we can set aside the boolean literals and work strictly with the expressions.

As a rule I never compare booleans directly.

if A === True

is just as easily handled by the Python (or any other language that supports truthy or falsy operations),

if A:

In the case of your first example, if this were inside a function,

return not not x

that will return a boolean if x is a value. If x is a relation (any comparison) then,

return x

It will still return a boolean since x is evaluated before the return. The literals need never appear anywhere in our source code.

Your second example makes nested comparisons with only a True outcome if all three variables are truthy. We can do this with logical operators:

return x and y and z

will return a boolean if at least z is an expression, and not value only, else it will return z as the value.

return x and y and not not z

will return a boolean regardless whether values or expressions.


If not x is a boolean toggle, as in the negation of the evaluation of x (and casting to boolean), then,

not not x

will always return the boolean that describes the truthy or falsy state of x

not 1 => False

not not 1 => True
1 Like

You can use below ex by considering timezone on console

from datetime import datetime
import pytz
now = datetime.now(pytz.timezone(‘Asia/Kolkata’))

print ‘%02d-%02d-%04d’ % (now.month, now.day, now.year)
print ‘%02d:%02d:%04d’ % (now.hour, now.minute, now.second)

Is it possible to format times by the 12 hour clock, rather than the 24 hour clock?

Yes you can.

Sorry for not answering your question fully. I don’t know how to do it, but maybe this links help.

Edit: :sweat: this links do not exactly answer your question. Anyway, I’ve seen that question being asked and answered in a previous thread from the Python 2 course Unit 2; try to check every recommended thread from that module and see all the posts (if you have time)…