B
B
bobs322020-02-29 02:16:43
PHP
bobs32, 2020-02-29 02:16:43

How to convert Russian time to datetime correctly?

Error when executing code

locale.setlocale(locale.LC_TIME, "ru_RU.UTF-8")
datetime.datetime.strptime('1 Декабрь 2019', '%d %B %Y').date()


ValueError: time data '1 Dec 2019' does not match format '%d %B %Y'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2019-07-11
@kakisaebalsujit

$db->query("INSERT INTO reg (vk_id, nfirst, nlast, bdate) VALUES ('$ids', '$first', '$last', '$bdate')");

ZY admin and strator

A
Alex, 2019-07-11
@streetflush

$db->query("INSERT INTO reg (vk_id, nfirst, nlast, bdate) VALUES ('$ids', '$first', '$last', '$bdate')");

S
Sergey Karbivnichy, 2020-02-29
@bobs32

Changed a bit:

from datetime import datetime

RU_MONTH_VALUES = {
    'Январь': 1,
    'Февраль': 2,
    'Март': 3,
    'Апрель': 4,
    'Май': 5,
    'Июнь': 6,
    'Июль': 7,
    'Август': 8,
    'Сентябрь': 9,
    'Октябрь': 10,
    'Ноябрь': 11,
    'Декабрь': 12,
}

def int_value_from_ru_month(date_str):
    for k, v in RU_MONTH_VALUES.items():
        date_str = date_str.replace(k, str(v))

    return date_str


date_str = int_value_from_ru_month('1 Декабрь 2019')
print (date_str)

d = datetime.strptime(date_str, '%d %m %Y')
print(d)

5e59a29437394339471282.png
Original
Another option:
pip3 install dateparser
import dateparser

ruDate = '1 Декабрь 2019'
pyDate = dateparser.parse(ruDate)
print(pyDate)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question