1

I am looking to retrieve the next possible date for a weekday contained in a string. Complexity being that this weekday will be in foreign language (sv_SE).

In bash I can solve this using `dateround´:

startdate=$(dateround --from-locale=sv_SE -z CET today $startday)

Highly appreciate your guidance on how to solve this in Python.

Thank you very much!

2 Answers2

2

Dateparser has support for quite a few languages. You could parse the weekday to a datetime object then determine the next possible date available.

-- Edit --

 from dateparser import parse

 parse('Onsdag').isoweekday()  # 3

Now that you have the iso weekday, you can find the next possible date. You can refer to this to see how.

4d11
  • 307
  • 1
  • 11
0

It seems locale aliases are platform specific and case sensitive. I've windows. So locale will be sv_SE.

You can use babel for date/time conversion and is much more comprehensive than native locale module.

Babel is an integrated collection of utilities that assist in internationalizing and localizing Python applications, with an emphasis on web-based applications.

Which can be installed as:
pip install Babel

Once installed, we can use format_date , format_datetime , format_time utilities to format one language date , time to other.

You can use these utilities to convert date/time data between English and Swedish.

>>>import datetime
>>>from babel.dates import format_date, format_datetime, format_time

#Here we get current date time in an datetime object
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2017, 10, 31, 9, 46, 32, 650000)

#We format datetime object to english using babel
>>> format_date(now, locale='en')
u'Oct 31, 2017'

#We format datetime object to sweedish using babel
>>> format_date(now, locale='sv_SE')
u'31 okt. 2017'
>>> 
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • babel is great but it works the other way round vs. the problem I described. I needed to build a date object first which 4d11 solves with his answer. – Andreas Marquardt Oct 31 '17 at 17:43