Home > Back-end >  How to minus 1 minute in a variable with format as '%Y-%m-%dT%H:%M:%S'
How to minus 1 minute in a variable with format as '%Y-%m-%dT%H:%M:%S'

Time:02-28

I have a variable which holds the date & time as like = '2022-02-27T07:43:00'. (letter "T" is mandatory which is coming from my database by default. I still need that "T" in-between for all the variables). Now I need to minus 1 minute and assign that value to a new variable in the same format = '%Y-%m-%dT%H:%M:%S'.

CodePudding user response:

What you need is probably dateutil.parser.parse and also timedelta function to the trick.

from dateutil.parser import parse
from datetime import timedelta
string = "2022-02-27T07:43:00"
dateObject = parse(string)
oneMinuteBefore = dateObject - timedelta(minutes=1)
oneMinuteBefore.strftime("%Y-%m-%dT%H:%M:%S")

Output

2022-02-27T07:42:00
  • Related