-1

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
zampa
  • 63
  • 1
  • 10
  • 3
    Does this answer your question? [Parse date string and change format](https://stackoverflow.com/questions/2265357/parse-date-string-and-change-format) – Esther Mar 23 '22 at 20:21
  • 1
    you'll want to use the python datetime library, https://docs.python.org/3/library/datetime.html, to parse the first date using a specified format string, and then print it with a different format string. – Esther Mar 23 '22 at 20:22
  • @Esther No , I want diff output – zampa Mar 23 '22 at 20:27
  • @Esther didn't get can you please give a small example ? – zampa Mar 23 '22 at 20:27

1 Answers1

2

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.

Corralien
  • 109,409
  • 8
  • 28
  • 52