0

I want to get the datetime(YYYY-MM-DD HH:MM) from 10 minutes ago, but when I use timedelta, it converts it to string. Is there a way to make this simpler?

from datetime import datetime,timedelta

startRaw = datetime.now() - timedelta(minutes=10)
start = startRaw.strftime('%Y-%m-%d %H:%M')

print(type(start))
<type 'str'>

start = datetime.strptime(start,'%Y-%m-%d %H:%M')

print(type(start)) 
<type 'datetime.datetime'>


print(start) 
2020-01-21 19:48:00
1
  • 1
    timedelta do not convert datetime to string. you convert it here start = startRaw.strftime('%Y-%m-%d %H:%M') Commented Jan 21, 2020 at 20:08

1 Answer 1

1

As @LinnTroll state, the start = startRaw.strftime('%Y-%m-%d %H:%M') converts the datatime object to string. You can just simply use the starRaw.

startRaw = datetime.now() - timedelta(minutes=10)
print(type(startRaw))
print(startRaw)

The outputs are:

<class 'datetime.datetime'>
2020-01-21 12:04:12.40710
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.