Python get this year

Get the Current Year in Python (With Examples)

Get the Current Year in Python (With Examples)

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)

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)

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)

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)

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.

Источник

Get the Current Year in Python

Get the Current Year in Python

  1. Get the Current Year in Python With strftime in the datetime Module
  2. Get the Current Year in Python With date.year in the datetime Module

Date and time are essential metrics that we all see daily. As developers, we need to play around with date and time for several tasks such as storing birth dates and finding the difference between dates in days. This article will introduce how to get the current year in Python.

Python has many in-built libraries at our service and tons of external libraries made by developers to get things done pretty fast and with understandable and straightforward syntax. One such module is datetime — an in-build python module to deal with date and time.

The datetime makes it very easy to deal with dates, and it provides a simple syntax. Let’s look at some possible ways to get the current year using this module.

Get the Current Year in Python With strftime in the datetime Module

The datetime module, as the name suggests, allows us to deal with both date and time. For this article, we’ll only focus on the date.

The following code demonstrates one way to print the current year.

import datetime  currentDateTime = datetime.datetime.now() date = currentDateTime.date() year = date.strftime("%Y") print(f"Current Year -> year>") 

We first import the module and then get the current date and time using the dateime.now() . To get the date from this result, we call the date() function on this result and store the returned date in a variable date . Next, we have to filter the year out from this date. For that, we use the strftime() function, which stands for string from time . This function takes a string argument that defines the kind of output, in the form of a string, we want from this object. In simple terms, it accepts a format in the form of a string and returns a string in the same format.

In line 5, the string format %Y means that we want the year from this date object. The string format %A refers to the weekday of the date object. We can also club multiple formats into a single string like this, %A %Y . This format returns a string with a weekday and a year like Wednesday 2021 .

Lastly, we store the string into a variable year and print it.

Refer to the official documents, here, for more possible formats.

Another way to get the current year is as follows.

import datetime  date = datetime.date.today() year = date.strftime("%Y") print(f"Current Year -> year>") 

This time we are using the date.today() method to get today’s date. Next, we use the strftime() function to get the year from the date and finally, print it.

Get the Current Year in Python With date.year in the datetime Module

If you don’t wish to use the strftime() function, you can use the following code to get the current year.

import datetime  currentDateTime = datetime.datetime.now() date = currentDateTime.date() print(f"Current Year -> date.year>")  date = datetime.date.today() print(f"Current Year -> date.year>") 
Current Year -> 2021 Current Year -> 2021 

The date() function in line 3 returns an object of type datetime.date . And this object has year as an attribute. So, we can access it and print out the result. Same goes with the today() function. It also returns an object of type datetime.date .

You can confirm that by running the print(type(date)) statement. It will print

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article — Python DateTime

Источник

e Learning

In this tutorial, we will learn how to get the current year in python 3 using the datetime module.

python get current year

To get the current year, we need to import the datetime submodule from the datetime module.

from datetime import datetime

After importing the datetime module, we can print the current year as follows:

from datetime import datetime print(datetime.today().year)

If you want to get the current year in two-digit format, use the .strftime method as follows:

print(datetime.today().strftime("%y"))

How it works…

In the earlier example, first, we imported the datetime submodule, which is part of the datetime module (both the main module and sub-module have the same name).

Python 3 datetime submodule comes with a method called today() , which holds information about the current time.

We want the current year, which we can get by appending .year to the today() method.

from datetime import datetime print(datetime.today().year)

The same way we can get the current month, day, hour, minute, and second.

from datetime import datetime current_year = datetime.today().year #Current Year current_month = datetime.today().month #Current Month current_day = datetime.today().day #Current Day current_hour = datetime.today().hour #Current Hour current_minute = datetime.today().minute #Current Minute current_second = datetime.today().second #Current Second

In the above Python code, we stored the current year, month, and date in separate variables.

Источник

Оцените статью