Python Get Current Date Minus a Hour

Python

Nicesnippets

132

13-11-2021


Python Get Current Date Minus a Hour

Hello Friends,

Now let's see example of how to get current date minus 1 hour in python. I am going to show you python get current date sub hour. We will learn get current date minus a hour using python.

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

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

Example 1


# current date minus a hour

from datetime import datetime, timedelta

currentTimeDate = datetime.now() - timedelta(hours=1)

# minus 1 hour

currentTime = currentTimeDate.strftime('%H:%M:%S')

print(currentTime)

Output:

14:02:05

Example 2

# current date minus a hour

from datetime import datetime

# minus number of hour

myDateTime = datetime.strptime('2021-09-25 12:06:50', '%Y-%m-%d %H:%M:%S')

myTime = myDateTime.strftime('%H:%M:%S')

print(myTime)

print(currentTime)

Output:

14:02:05

It will help you....


Recommended Posts