Python Dates: A Complete Guide
Python dates are an essential aspect of programming when it comes to managing and handling date and time requirements. In this article, we’ll provide you with a comprehensive guide on Python dates, including various date functions, formatting options, and more.
Working with Python Dates
Working with Python dates involves importing the ‘datetime’ module, which contains functions and classes to manipulate and manage dates and times. Here’s how to import the datetime module in Python:
Getting the Current Date and Time
To get the current date and time in Python, use the ‘datetime’ class and its ‘now()’ method as shown below:
import datetime current_time = datetime.datetime.now() print("The current date and time is:", current_time)
You should see the current date and time displayed like this:
The current date and time is: 2021-10-25 13:17:20.303526
Formatting Dates and Times with strftime()
Python provides the strftime() method to format dates and times as per your requirements. The strftime() method takes a formatted string argument that contains various format codes indicating the date and time elements you want to display.
Here’s an example:
import datetime current_time = datetime.datetime.now() print("The current date and time is:", current_time.strftime("%a-%b-%Y %I:%M:%S %p"))
The output should now look like this:
The current date and time is: Mon-Oct-2021 01:17:20 PM
The ‘%a’, ‘%b’, ‘%Y’, ‘%I’, ‘%M’, ‘%S’, and ‘%p’ are all format codes that represent the abbreviated weekday, month name, four-digit year, hour in 12-hour format, minute, second, and AM/PM respectively.
Here’s a list of some common format codes for date and time formatting:
|%a||Abbreviated weekday name||Mon|
|%A||Full weekday name||Monday|
|%b||Abbreviated month name||Oct|
|%B||Full month name||October|
|%c||Locale’s appropriate date and time representation||Mon Oct 25 13:17:20 2021|
|%d||Day of the month as a decimal number (01-31)||25|
|%H||Hour (24-hour clock) as a decimal number (00-23)||13|
|%I||Hour (12-hour clock) as a decimal number (01-12)||01|
|%j||Day of the year as a decimal number (001-366)||298|
|%m||Month as a decimal number (01-12)||10|
|%M||Minute as a decimal number (00-59)||17|
|%p||Locale’s equivalent of either AM or PM.||PM|
|%S||Second as a decimal number (00-59)||20|
|%U||Week number of the year (Sunday as the first day of the week) as a decimal number (00-53)||43|
|%w||Weekday as a decimal number (0-6, where 0 is Sunday)||1|
|%W||Week number of the year (Monday as the first day of the week) as a decimal number (00-53)||43|
|%x||Locale’s appropriate date representation||10/25/21|
|%X||Locale’s appropriate time representation||13:17:20|
|%y||Year without century as a decimal number (00-99)||21|
|%Y||Year with century as a decimal number||2021|
|%z||UTC offset in the form +HHMM or -HHMM||+0530|
|%Z||Timezone name or abbreviation||IST|
|%%||A literal % character||%|
You can use these format codes in different combinations to get various date and time formats.
Converting Strings to Dates with strptime()
Apart from formatting dates and times, Python also allows you to parse date strings into datetime objects using the strptime() method. The method takes a string and a format string as input and returns a datetime object.
Here’s an example:
import datetime date_str = "Oct 25, 2021" date = datetime.datetime.strptime(date_str, "%b %d, %Y") print("The date is:", date)
The output should look something like this:
The date is: 2021-10-25 00:00:00
In the above example, we’re parsing the ‘date_str’ string into a datetime object using the format string “%b %d, %Y”, which matches the “Oct 25, 2021” pattern.
Python dates are one of the most fundamental aspects of programming that come in very handy when working with programs that involve any date and time requirements. In this article, we covered some of the most important date functions and how to format dates and times in Python, making it easy for you to work with dates in any programming project.