简体   繁体   中英

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

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'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM