×

How to get current date in python?

How to get current date in python?

In Python programming language, date and time are not just a data form, but it is possible to import a method called datetime to operate with both the date and time. The Datetime element is integrated with Python, so it does not require to be installed individually.

The datetime module offers some operations for obtaining both the current date and time. Let's try them out.

date.today() method

today() method of date class lies inside the module of datetime. This method is used to give a date object that give us the value of today’s date. It returns the current local date.

Syntax

Here, we displayed the syntax of above method.

date.today()

Example

 // Python program to get current date
 from datetime import date     //Importing date class from datetime module
 today = date.today()
 print("current local date is:", today)       // Returns the current local date 

Output

After the successful execution of the above program code, we got the following output.

datetime.now()

Python Library describes the ability that camwood should be used primarily to get the existing opportunity when it's also a date. Now() method takes advantage of the current date and time of the neighborhood, which can be specified in the datetime module. This method returns the existing time and date in time format.

Syntax

Here, we have displayed the syntax of above method.

datetime.now(tz)

Parameters

tz: It will be used to specify the time zone of the existing possibility; an additional date may be required. (Uses Greenwich meridian run through Toward default).

 from datetime import datetime          // Importing datetime class from datetime module
 X = datetime.now()                            // returns current date and time
 Print = (“X =”, X) 

Output

We got the following output, after successful execution of the above program code.

Attributes of now() function                                                        

The now() method contains several attributes including time such as year, month, hour, minute, second.

Example  

Here, an example code is given below to display the attributes of now().

 // attributes of now()
 import datetime                                                   // importing datetime module for now()
 current_time = datetime.datetime.now()            // utilizing now() to get the current time
 print ("The attributes of now() are displayed below : ")      // Printing attributes of now()
 print ("Year : ", end = "")
 print (current_time.year)
 print ("Month : ", end = "")
 print (current_time.month)
 print ("Day : ", end = "")
 print (current_time.day) 

Output

After the successful execution of the above code, we got the below given output.

Strftime() method

The datetime entity contains a procedure for syntax date artifacts into comprehensible strings.

The method is considered as strftime(), and requires only single parameter, form, to clarify the template of the retrieved string:

Example:1

Here, we have example to show the name of the month.

 Import datetime                 // Importing datetime module for Strftime() method
 a = datetime.datetime (2021, 02, 24)
 print = (a.strftime(“%B”))                            // Printing the name of the month 

Output

After the execution of the above program code, we got the below displayed output.

Example: 2

Here, we have example to show the year and the name of weekday.

 import datetime          // importing datetime module for strftime() method
 y = datetime.datetime.now()
 print(y.year)
 print(y.strftime(“%A”))    // printing year and name of the weekday 

Output

We got the following output, after the successful execution of the above program code.


Related Topics

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Adding a key-value pair to dictionary in Python

Python dictionaries are collections of unsorted key-value pairs. This article will examine a method for adding new key-value teams to an existing dictionary. Dictionary in Python With the aid of curly brackets...

3 minutes read.

Python Constructor

Introduction A constructor is defined as the special kind of function or method that is used for instance variables initialization during the creation of an object of a class. The constructor's task...

5 minutes read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Yield Statement In Python

The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns back the...

2 minutes read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

Get Bounding Box Co-ordinates Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.