0

I am trying to write a code to extract the date and time of the last email received in my inbox to a text file. Current format of the output from my variable b is '2020-07-18 16:53:10.444000+00:00'. I want the format to be in this format '%m/%d/%y %H:%M:%S'. Appreciate of someone could help me.

Also would be great to know what the last 5 digits (+00:00) are? I am trying to understand the default format of [ReceivedTime]

b = str(lastDayMessages[0].ReceivedTime)
b = dt.datetime.strptime(b, '%m/%d/%y %H:%M:%S')
print(b)

Error: raise ValueError("time data %r does not match format %r" % ValueError: time data '2020-07-18 16:53:10.444000+00:00' does not match format '%m/%d/%y %H:%M:%S'

FObersteiner
  • 22,500
  • 8
  • 42
  • 72

1 Answers1

1

strptime converts a string to a datetime. strftime converts a datetime to a string.

b = '2020-07-18 16:53:10.444000+00:00'
b = dt.datetime.strptime(b, '%Y-%m-%d %H:%M:%S.%f%z') 
b.strftime('%m/%d/%y %H:%M:%S')

Output

'07/18/20 16:53:10'

Read more: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Pramote Kuacharoen
  • 1,496
  • 1
  • 5
  • 6