Home > Net >  Creating datetime objects by delimiting provided time
Creating datetime objects by delimiting provided time

Time:07-27

I have been searching and found similar questions but am struggling particularly with this project.

I would like to create a datetime object from a date with the following format:

"07/19/22 7:35:15" or "07/06/22 7:54:46" or "06/28/22 10:39:24"

I understand how to delimit if I was doing the two separately but not sure how to delimit the entire string at once. Would it be best to separate them with a space and delimit them separately or is there a simpler route to go about this. Thanks for the help!

CodePudding user response:

import datetime
date = '07/19/22 7:35:15'
dt = datetime.datetime.strptime( date, '%m/%d/%y %H:%M:%S' )

CodePudding user response:

You can parse the whole thing in one go:

>>> from datetime import datetime
>>> datetime.strptime('07/19/22 7:35:15', '%m/%d/%y %H:%M:%S')
datetime.datetime(2022, 7, 19, 7, 35, 15)
  • Related