pythonstringdatetimearrow-python

Python change locale with arrow


I have a date string: "Viernes 24 de Octubre". I want to change it to arrow datetime object. also i have installed es locales: sudo apt-get install language-pack-es-base This doesn't work:

print arrow.get('Viernes 24 Octubre', 'dddd D MMMM', locale='es')

Thank you


Solution

  • arrow.parser.DateTimeParser() uses calendar.month_name[1:] to parse month names i.e., you need to set locale before calling arrow.get():

    import calendar
    import locale
    
    print(calendar.month_name[10])
    # -> October
    locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8') # system-dependent locale name
    print(calendar.month_name[10])
    # -> Octubre
    

    Note: changing the locale affects the whole program.