Getting the current time in Python is a nice starting point for many time-related operations. One very important use case is creating timestamps. In this tutorial, you’ll learn how to get, display, and format the current time with the datetime
module.
To effectively use the current time in your Python applications, you’ll add a few tools to your belt. For instance, you’ll learn how to read attributes of the current time, like the year, minutes, or seconds. To make the time more easily readable, you’ll explore options for printing it. You’ll also get to know different formats of time and learn how computers represent time, how to serialize time, and how to deal with time zones.
Source Code:Click here to download the free source code for getting and using the current time in Python.
How to Tell the Time in Python
The most straightforward way to get and print the current time is to use the .now()
class method from the datetime
class in the datetime
module:
>>> fromdatetimeimportdatetime>>> now=datetime.now()>>> nowdatetime(2022, 11, 22, 14, 31, 59, 331225)>>> print(now)2022-11-22 14:31:59.331225
The class method .now()
is a constructor method that returns a datetime
object. When the REPL evaluates the now
variable, you get a representation of the datetime
object. It can be pretty hard to tell what each number means. But if you explicitly print the now
variable, then you get a slightly different output that presents the information in a familiar timestamp format.
Note: The datetime
object that you get here isn’t time zone aware. Usually your operating system can resolve the time zone correctly, but the datetime
object itself currently has no time zone information. You’ll get into time zone–aware objects in a later section of this tutorial.
You may recognize the format of the printed datetime
object. It closely follows an international standard, ISO 8601, for formatting time and dates. You’ll find this format in many places!
There’s a slight deviation from the ISO 8601 standard in the format that Python uses, though. The standard says that the date and the hour parts of the timestamp should be separated by a T
character, but the default datetime
object passed through the print()
function separates them with a single space.
Python, being ever extensible and customizable, enables you to customize the format in which it prints the timestamp. The datetime
class internally uses its .isoformat()
method when printing. Since .isoformat()
is just an instance method, you can call it directly from any datetime
object to customize the ISO timestamp:
>>> datetime.now().isoformat()'2022-11-22T14:31:59.331225'>>> datetime.now().isoformat(sep="")'2022-11-22 14:31:59.331225'
You’ll note that when you call .isoformat()
without any arguments, the standard ISO 8601 separator T
is used. The way that the datetime
class has implemented its special instance method .__str__()
under the hood, though, is with a single space as the sep
argument.
Being able to get the full date and time is great, but sometimes you might be looking for something specific. Maybe you only want the month or day, for example. In those cases, you can choose from a bunch of attributes:
>>> fromdatetimeimportdatetime>>> now=datetime.now()>>> print(f"""... {now.month= }... {now.day= }... {now.hour= }... {now.minute= }... {now.weekday()= }... {now.isoweekday()= }"""... )now.month = 11now.day = 22now.hour = 14now.minute = 31now.weekday() = 1now.isoweekday() = 2
In this snippet, you use a triple-quoted f-string with the =
sign within the curly brackets to output the expressions and their results.
Read the full article at https://realpython.com/python-get-current-time/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]