0

I wanted to convert a date from git log but I'm trying to match the date from the documentation of datetime but it never matches.

from datetime import datetime
receivedDate = (commits[0]['Date']) #receiving date
print(receivedDate)      #e.g Thu Jan 14 12:47:30 2016 +0100      
receivedDate = ' '.join(receivedDate.split(' ')[:-1])  #removing +0100
date = datetime.strptime(receivedDate,'%a %b %d %H:%M:%S %Y')

ValueError: time data 'Thu Jan 14 12:47:30 2016' does not match format '%a %b %d %H:%M:%S %Y'

I've also tried by keeping '+0100' and added %z, but it doesn't work either. It runs with python 3.6.


Thanks for help or any idea :)

Ioan S.
  • 154
  • 3
  • 14
  • Possible duplicate of [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – shmee Apr 30 '19 at 08:21

1 Answers1

1

You can use python-dateutil where you don't need to provide a format string.

from dateutil import parser
print(parser.parse('Thu Jan 14 12:47:30 2016'))
#2016-01-14 12:47:30

Also I am able to use your datetime format as well.

import datetime
print(datetime.datetime.strptime('Thu Jan 14 12:47:30 2016', '%a %b %d %H:%M:%S %Y'))
#2016-01-14 12:47:30
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40