Python Program to Display the Current Date and Time

Python program to display the current date and time; Through this tutorial, i am going to show you how to print or display the current date and time in python.

Python Program to Display the Current Date and Time

See the following python program or code example for get and display the current date and time in python; as shown below:

  • 1: How to display current date and time in python?
  • 2: How to the current date in python?
  • 3: Date object to represent a date in python
  • 4: Get current date in python
  • 5: Get date from a timestamp python
  • 6: Print today’s year, month and day in Python DateTime
  • 7: Time object to represent time in Python
  • 8: Display hour, minute, second and microsecond Python
  • 9: Python datetime object
  • 10: Display year, month, hour, minute and timestamp in Python

1: How to display current date and time in python?

Import DateTime module in your python program. Then you can use now() method to create a datetime object containing the current local date and time.

#import datetime module
]import datetime
datetime_object = datetime.datetime.now()
print(datetime_object)

After executing the program, the output will be something like:

 2021-04-26 11:00:45.188915 

In the above python program, have imported datetime module using import datetime statement. One of the classes defined in the datetime module is datetime class. Then used now() method to create a datetime object containing the current local date and time.

2: How to the current date in python?

Import datetime module in your python program. And you can use today() method defined in the date class to get a date object containing the current local date.

import datetime
date = datetime.date.today()
print('Today is ', date)

After executing the program, the output will be something like:

 Today is 2020-04-26 

In this program, have used today() method defined in the date class to get a date object containing the current local date.

3: Date object to represent a date in python

import datetime
d = datetime.date(2020, 4, 13)
print(d)

After executing the program, the output will be:

2020-04-13

4: Get current date in python

You can create a date object containing the current date by using a classmethod named today(). Here’s how:

from datetime import date
today = date.today()
print("Current date =", today)

After executing the program, the output will be:

 Current date = 2020-04-26 

5: Get date from a timestamp python

You can also create date objects from a timestamp. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. You can convert a timestamp to date using fromtimestamp() method.

from datetime import date
timestamp = date.fromtimestamp(1326244364)
print("Date =", timestamp)

After executing the program, the output will be:

Date = 2012-01-11

6: Print today’s year, month and day in Python DateTime

Get year, month, day, day of the week etc. from the date object. See the following python program:

from datetime import date
# date object of today's date
today = date.today() 
print("Current year:", today.year)
print("Current month:", today.month)
print("Current day:", today.day)

After executing the program, the output will be:

Current year: 2020
Current month: 4
Current day: 26

7: Time object to represent time in Python

from datetime import time
# time(hour = 0, minute = 0, second = 0)
a = time()
print("a =", a)
# time(hour, minute and second)
b = time(12, 34, 56)
print("b =", b)
# time(hour, minute and second)
c = time(hour = 9, minute = 34, second = 56)
print("c =", c)
# time(hour, minute, second, microsecond)
d = time(1, 34, 56, 234566)
print("d =", d)

After executing the program, the output will be:

a = 00:00:00 
b = 12:34:56 
c = 09:34:56 
d = 01:34:56.234566 

8: Display hour, minute, second and microsecond Python

Once you create a time object in your python programs, you can easily display its properties such as hour, minute etc.

from datetime import time
a = time(11, 34, 56)
print("hour =", a.hour)
print("minute =", a.minute)
print("second =", a.second)
print("microsecond =", a.microsecond)

After executing the python program, the output will be:

hour = 11
minute = 34
second = 56
microsecond = 0

Note:- The microsecond default value 0.

9: Python datetime object

from datetime import datetime
#datetime(year, month, day)
a = datetime(2018, 11, 28)
print(a)
# datetime(year, month, day, hour, minute, second, microsecond)
b = datetime(2017, 11, 28, 23, 55, 59, 342380)
print(b)

After executing the program, the output will be:

2018-11-28 00:00:00 2017-11-28 23:55:59.342380 

10: Display year, month, hour, minute and timestamp in Python

from datetime import datetime
a = datetime(2020, 10, 30, 23, 55, 59, 342380)
print("year =", a.year)
print("month =", a.month)
print("hour =", a.hour)
print("minute =", a.minute)
print("timestamp =", a.timestamp())

After executing the program, the output will be:

year = 2020
month = 10
hour = 23
minute = 55
timestamp = 1604102159.34238

Recommended Python Tutorials

Leave a Comment