简体   繁体   中英

Convert Date time to human readable formate python

I have a date time string something like this

2022-03-21 16:29:01.8593

but I want it to be like

 03/21/2022 02:16 PM

Use datetime module:

from datetime import datetime

d = datetime.strptime('2022-03-21 16:29:01.8593', '%Y-%m-%d %H:%M:%S.%f')
s = d.strftime('%m/%d/%Y %I:%M %p')

Output:

>>> d
datetime.datetime(2022, 3, 21, 16, 29, 1, 859300)

>>> s
'03/21/2022 04:29 PM'

Use this link as documentation.

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