0

I have DateTime String in this format "2018-02-08T23:59:05.823Z" I know 'T ' is separating date from time, I have split the string on 'T' delimiter and I got Time - > '23:59:05.823Z'

I want to convert this time '23:59:05.823Z' to this format like '10:00 AM'

How can we do that in python ?

Faraz Irfan
  • 1,306
  • 3
  • 10
  • 17

2 Answers2

2

I want to convert this time '23:59:05.823Z' to this format like '10:00 AM'

looks like you are using ISO date time format. You could do something like this.

>>> from datetime import datetime
>>> d = datetime.strptime("23:59:05.823Z", "%H:%M:%S.%fZ")
>>> d.strftime("%I:%M %p")
'11:59 PM'
Kunal Pal
  • 545
  • 8
  • 21
1

If you are using the Python datetime module then you can use the .strftime() method to convert you datetime into a string of your desired format. For example:

from datetime import datetime

current_datetime = datetime.now()

print(current_datetime)
>>> 2018-02-17 09:23:31.079326

print(current_datetime.strftime("%I:%M %p")
>>> 09:23 AM

%I gives you the hour in a 12-hour clock as a zero-padded number, %M gives you the minute as a zero-padded number, and %p gives you AM or PM. You can read more about this in the official documentation: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Studybuffalo
  • 399
  • 4
  • 8