Python Get Current Date And Time Example

Python

Nicesnippets

14-12-2021


Python Get Current Date And Time Example

Hello Friends,

Now let's see example of how to get current datetime in python. I am going to show you python get current date and time. We will learn get current datetime in python.

The datetime module is supplies classes for manipulating dates and times. This article will give you example of get current datetime in python.

Here I will give three example for how to get current datetime in python. I am use datetime and time module to get current current date time. So let's see the below example:

Example 1


example1.py

# import datetime module

from datetime import datetime

currentTimeDate = datetime.now()

print(currentTimeDate)

Run Example

python example1.py

Output:

2021-12-14 16:25:25.425925

Example 2

example2.py

# import datetime module

from datetime import datetime

currentTimeDate = datetime.now()

currentTimeDate = currentTimeDate.strftime("%d/%m/%Y %H:%M:%S")

print(currentTimeDate)

Run Example

python example2.py

Output:

14/12/2021 16:26:06

Example 3

example3.py

# import datetime module

import datetime

# import pytz module for timezone()

import pytz

current_time = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))

print(current_time)

Run Example

python example3.py

Output:

2021-12-14 16:26:52.295001+05:30

It will help you....


Recommended Posts