How do i print the current day in python?

In this article, you will learn to get today's date and current date and time in Python. We will also format the date and time in different formats using strftime() method.

Video: Dates and Times in Python

There are a number of ways you can take to get the current date. We will use the date class of the datetime module to accomplish this task.


Example 1: Python get today's date

from datetime import date

today = date.today()
print("Today's date:", today)

Here, we imported the date class from the datetime module. Then, we used the date.today() method to get the current local date.

By the way, date.today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.


Example 2: Current date in different formats

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)

When you run the program, the output will be something like:

d1 = 16/09/2019
d2 = September 16, 2019
d3 = 09/16/19
d4 = Sep-16-2019

If you need to get the current date and time, you can use datetime class of the datetime module.

Example 3: Get the current date and time

from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
 
print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)	

You will gate output like below.

now = 2021-06-25 07:58:56.550604
date and time = 25/06/2021 07:58:56

Here, we have used datetime.now() to get the current date and time. Then, we used strftime() to create a string representing date and time in another format.

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

    Prerequisite: DateTime module

    In Python, Date and Time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. The DateTime module provides some functions to get the current date as well as time.

    Get the current date using date.today()

    The today() method of date class under DateTime module returns a date object which contains the value of Today’s date.

    Syntax: date.today() 

    Returns: Return the current local date.

    Example: 

    Python3

    from datetime import date

    today = date.today()

    print("Today date is: ", today)

    Output:

    Today date is:  2019-12-11

    Get the current date using datetime.now()

    Python library defines a function that can be primarily used to get the current time and datetime.now() function and return the current local date and time, which is defined under the DateTime module.

    Syntax: datetime.now(tz) 

    Parameters : tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.) Returns : Returns the current date and time in time format.

    Example:

    Python3

    from datetime import datetime

    now = datetime.now()

    print("now = ", now)

    Output:

    now =  2019-12-11 10:58:37.039404

    Attributes of now()

    The now() has different attributes, same as attributes of time such as year, month, date, hour, minute, and second. 

    Example

    Python3

    import datetime

    current_time = datetime.datetime.now()

    print ("The attributes of now() are : ")

    print ("Year: ", end = "")

    print (current_time.year)

    print ("Month: ", end = "")

    print (current_time.month)

    print ("Day: ", end = "")

    print (current_time.day)

    Output:

    The attributes of now() are: 
    Year: 2019
    Month: 12
    Day: 11

    RECOMMENDED ARTICLES – Get Current Date and Time using Python


    How do I print present day in Python?

    Approach:.
    In Python, in order to print the current date consisting of a year, month, and day, it has a module named datetime. ... .
    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..

    How do I get today's month and day in Python?

    you will do the following things for python get current month as decimal number. from datetime import datetime today = datetime. now() month1 = today. strftime("%b") print("Current Month Full Name:", month1); month2 = today.

    How do I print today's date in pandas?

    Get the current date and time from Timestamp object, use the timestamp..
    import pandas as pd import datetime. Create the timestamp in Pandas..
    timestamp = pd.Timestamp(datetime.datetime(2021, 10, 10)) Display the Timestamp..
    print("Timestamp: ", timestamp) ... .
    res = timestamp.today().

    Which function is used to print current date and time Python?

    To get both current date and time datetime. now() function of DateTime module is used. This function returns the current local date and time.