-1

I work with api in python3 in this api return date like this

'Jun 29, 2018 12:44:14 AM'

but i need just hours, minute ad second like this

12:44:14

are there a fonction that can format this

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
john
  • 468
  • 1
  • 9
  • 26
  • 6
    Possible duplicate of [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – eyllanesc Jun 28 '18 at 22:52

2 Answers2

1

It looks like the output is a string. So, you can use string slicing:

x = 'Jun 29, 2018 12:44:18 AM'
time = x[-11:-3]

It's best to use negative indexing here because the day may be single-digit or double-digit, so a solution like time = x[13:21] won't work every time.

If you're inclined, you may wish to use strptime() and strftime() to take your string, convert it into a datetime object, and then convert that into a string in HH:MM:SS format. (You may wish to consult the datetime module documentation for this approach).

Joel
  • 1,564
  • 7
  • 12
  • 20
1

Use the datetime module. Use .strptime() to convert string to datetime object and then .strftime() to convert to your required string output. Your sample datetime string is represented as '%b %d, %Y %H:%M:%S %p'

Ex:

import datetime
s = 'Jun 29, 2018 12:44:14 AM'
print( datetime.datetime.strptime(s, '%b %d, %Y %H:%M:%S %p').strftime("%H:%M:%S") )

Output:

12:44:14
Rakesh
  • 81,458
  • 17
  • 76
  • 113