1

I can't parse a date written in italian:

 datetime.strptime("sab 21 mar 2020, 13:04", "%a %d %b %Y, %H:%M")

I got this error:

ValueError: time data 'sab 21 mar 2020, 13:04' does not match format '%a %d %b %Y, %H:%M'

If I try with:

datetime.strptime("sat 21 mar 2020, 13:04", "%a %d %b %Y, %H:%M")

It's ok, so how can parse the date in the right local time? In Java, I did so:

SimpleDateFormat parser = new SimpleDateFormat("EEE dd MMM yyyy, HH:mm", Locale.ITALIAN);

with this line I was able to parse date correcly, does exist something similar in python?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
DarkSkull
  • 1,041
  • 3
  • 13
  • 23
  • 1
    Does this answer your question? [Locale date formatting in Python](https://stackoverflow.com/questions/985505/locale-date-formatting-in-python) – Riccardo Bucco Mar 24 '20 at 13:41
  • This isn't a duplicate of the linked question because this is about _parsing_ not about _formatting_. – BlackJack Jul 21 '20 at 18:54

1 Answers1

2

Just set the locale to the appropriate region.

import datetime
import locale

locale.setlocale(locale.LC_TIME, "italian")
print(datetime.datetime.strptime("sab 21 mar 2020, 13:04", "%a %d %b %Y, %H:%M"))

OUTPUT

2020-03-21 13:04:00
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42