Hướng dẫn get current year python

In this tutorial, we are going to learn about how to get the current year in Python.

Getting the current year

To get the current year in Python, first we need to import the date class from the datetime module and call a today().year on it.

The year property returns the current year in four-digit(2021) string format according to the user’s local time.

Here is an example:

from datetime import date

current_year = date.today().year
print(current_year)

Output:

2021

We all regularly observe the date and time, which are crucial measurements. As programmers, we frequently need to fiddle with dates and times for various tasks like recording birth dates and calculating the disparity between dates in days. Many a time we want to access the current date and perform some operations on it to get some results. In this article, we are going to learn how to get the current year in python.

What is DateTime Module?

To get things done quickly and with clear and simple syntax, Python has a tonne of built-in libraries at our disposal in addition to a tonne of external libraries created by developers. Datetime is one such module; it is a built-in Python module that deals with date and time.

The datetime provides a straightforward syntax and simplifies dealing with dates. Let's examine some potential methods for obtaining the current year with this module.

How to Get Current Year in Python?

There are two methods to get the current year in Python. Let's look at them.

1) By Using today() method

We use the today() method inside the date class to fetch today's date. This class is present inside the datetime module.

For example:

import datetime

today = datetime.date.today()

print(today)

Output:

Now to just get the year from this returned date object we have two ways. Let's see them one by one.

a) Accessing the year attribute

Utilizing the datetime module is the quickest way to determine the current year. We can directly access the year attribute of the returned object.

For example:

import datetime

today = datetime.date.today()

year = today.year

print(year)

Output:

b) With srtftime() function 

The strfttime() function in Python is another method for obtaining the current year. The function strftime() takes a string specifying the date format as the argument. It returns the given date as a string in the provided format.

We will pass "% Y" to strftime() to get the year of the date object.

For example:

import datetime

today = datetime.date.today()

year = today.strftime("%Y")

print(year)

Output:

2) By Using now() method

To get the current date and time we can also use the now() method present inside the datetime class inside the datetime module. Then to get the current year, we can either access the year attribute or use the strftime() method

For example:

import datetime

today = datetime.datetime.now()

#accessing the year attribute
year1 = today.year
print(year1)

#using the strftime() method
year2 = today.strftime("%Y")
print(year2)

Output:

Conclusion

Python with its huge collection of built-in libraries makes a tedious task a very easy job. In this article, we learned two methods to get the current year in python which can be easily done with the datetime module. 

Check how to extract the year from the date in R Programming.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, the task is to write a Python Program to print the current year, month, and day.

    Approach:

    • In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. From the DateTime module, import date class
    • Create an object of the date class
    • Call the today( ) function of date class to fetch todays date.
    • By using the object created, we can print the year, month, day(attribute of date class) of today.

    Python3

    from datetime import date

    todays_date = date.today()

    print("Current date: ", todays_date)

    print("Current year:", todays_date.year)

    print("Current month:", todays_date.month)

    print("Current day:", todays_date.day)

    Output:

    Current date:  2020-12-10
    Current year: 2020
    Current month: 12
    Current day: 10