How to remove milliseconds from the time retrieved from the Python database and convert it to hour, minute, and second format

The time obtained from the database is in milliseconds, such as:

2021-03-27 18:59:05.373805

Its format is:

%Y-%m-%d %H:%M:%S.%f

So how to convert to the following format of hours, minutes and seconds?

2021-03-27 18:59:05

Its format is:

%Y-%m-%d %H:%M:%S

It's actually very simple. I don't appreciate the following code:

str_datetime = '2021-03-27 18:59:05.373805'
time1 = datetime.datetime.strptime(str_datetime, "%Y-%m-%d %H:%M:%S.%f")
print(time1, type(time1))
# 2021-03-27 18:59:05.373805 <class 'datetime.datetime'>
time2 = time1.strftime('%Y-%m-%d %H:%M:%S')
print(time2, type(time2))
# 2021-03-27 18:59:05 <class 'str'>

rememberLeave a lingering fragranceYeah~

Guess you like

Origin blog.csdn.net/WU2629409421perfect/article/details/115277563