×

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 len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

5 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

Python ascii() Function

Python ascii() Function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). This function will replace any non-ascii characters with escape characters. Syntax ascii(object) Parameter object:  An object, like String, List, Tuple, Dictionary, etc. Return This...

1 minute read.

Python IDE

What is an IDE?  An IDE or an Integrated Development Environment is a type of software where developers can develop many software’s and applications and run the programs inside it. An...

11 minutes read.

Exrex 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...

4 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.