This blog post will discuss a number of different methods to obtain the current month or year in Python. For working with dates or timestamps, the Python language’s datetime module is indeed very helpful. The datetime module is a built-in component that offers capabilities to manage a variety of complicated functions involving date and time. To work with date and time, it contains classes for date, time, timezone, and timedelta. To get the current month and year today, we’ll utilize the same module in a few multiple methodologies.
In Python, you can obtain the current year and month using the built-in datetime and time modules. Some methods for achieving this include using the datetime.now() and date.today() functions from the datetime module, or using the time module while leveraging the strftime() function to format the output.
To learn more about the datetime module of python language, please click here.
Method 1: Using datetime.now() Function
There are a number of submodules in the datetime package. As the .datetime.now() method is included in the datetime submodule, one must import it first. The date and time as of the current local computer are represented by the DateTime object returned by the .datetime.now() method. To limit the value that is extracted using this method, other attributes can be added. The word “year” followed by a dot is added to the method in order to determine the current year. Similarly, “month” after a dot is added to obtain the value of the current month. Following is the code for this method
import datetime from datetime import datetime month = datetime.now().month year = datetime.now().year print("Current month: ", month) print("Current year: ", year)
Take note that the output of the aforementioned code is in integer format. How about printing the name of the current month? Using strftime in addition to the previously mentioned method is the answer to this problem. When a format string is provided explicitly, the function strftime(format) generates a string that represents the time under its control.
In order to get the entire name of the month, the ‘%B” format is passed to strftime(), and to get the abbreviation of the current month, the “%b” format is passed.
Similar to this, the “%y” format is added to obtain the value of the current year without the century as a zero-padded decimal number (01, 22, 23..), while the “%Y” formatting is applied to obtain the value of the current year with the century as a decimal number (2001, 2022, 2023..) Following the code explaining the same
To learn more about strftime(), please click here
month_abbrev = datetime.now().strftime("%b") month_text = datetime.now().strftime("%B") print("Current month: ", month_abbrev, month_text) year_short = datetime.now().strftime("%y") year_entire = datetime.now().strftime("%Y") print("Current year: ", year_short, year_entire)
Method 2: Using date.today() Function
The date module is one of the multiple submodules that make up the datetime module, therefore we need to import the date submodule before moving further with the implementation. It has a function named date.today() that returns the current date of the given location.
The current month or year can also be obtained using this method. It operates quite similarly to the datetime.now() method that was previously covered. The use of strftime(format) is possible in this case also. Following is the code of this method
import datetime from datetime import date month = date.today().month month_abbrev = date.today().strftime("%b") month_text = date.today().strftime("%B") print("Current month: ", month, month_abbrev, month_text) year = date.today().year year_short = date.today().strftime("%y") year_entire = date.today().strftime("%Y") print("Current year: ", year, year_short, year_entire)
Method 3: Using the Time Module in Python
The Python time module offers a variety of coding representations for time, including objects, integers, and strings. The same module can be used along with strftime(format) to obtain the values of the current month or current year.
To learn more about the time module of the python language, please click here.
import time month = time.strftime("%m") month_text = time.strftime("%B") print("Current month: ", month, month_text) year = time.strftime("%Y") year_short = time.strftime("%y") print("Current year: ", year, year_short)
Python is a great interpreted language. Besides its multiple applications and complex use cases, it has many built-in modules which help with working on different types of objects like date and time. In this article, we discussed three different ways – two using the submodules of the datetime package and one using the time module of the python programming language.
To learn from more such detailed and easy-to-understand articles related to various topics related to Python Programming Language, please click here.