Home > Net >  How to replace space from Python datetime object between date and time
How to replace space from Python datetime object between date and time

Time:01-06

I am looking to remove the space separating the date and the time from a python datetime object. I am using strptime "%Y-%m-%dT%H:%M:%S.%f", so I do not know why there is a space included to begin with.

Code:

import datetime
start_timestamp = "2022-11-23T10:08:00.000"
date_time_start = datetime.datetime.strptime(start_timestamp, "%Y-%m-%dT%H:%M:%S.%f")
print(date_time_start)

Output: 2022-11-23 10:08:00

Desired output: 2022-11-23_10:08:00

CodePudding user response:

Use isoformat with custom separator:

>>> date_time_start.isoformat(sep="_")
'2022-11-23_10:08:00'

CodePudding user response:

The simplest way is to use this code:

print(date_time_start[1:]

This removes the first character in a string.

  • Related