Converting date name into numeric value

Viewed 25

I am writing a function that has the whole purpose to convert data given to a numeric date in order to compare it further in different functions.

Example of data in <class 'list'> format:

['2016 Jan 04, 11:50']
['2016 january 04, 11:50']

Actual code:

def date(data):
    old_months = [
        ['january', 'febuary', 'march',
         'april', 'may', 'june',
         'july', 'august', 'september',
         'october', 'november', 'december'],
        ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']]

    new_months = [
        '01', '02', '03', '04', '05', '06',
        '07', '08', '09', '10', '11', '12']
    
    for x in range(len(new_months)):
        for i in range(len(old_months)):
            data = [item.replace(old_months[i][x], new_months[x]) for item in data]
    data[1] = data[1].split()[::-1]
    data[1] = " ".join([str(elem) for elem in data[1]])
    return data[1], data[2]

This code works:

  1. sometimes for data with 2016 Jan 04, 11:50 format
  2. always for data with2016 january 04, 11:50 format

What do I do wrong with the code? How can I fix it? Month names will be in non-english language. Does changes in data format would fix this problem of mine?

0 Answers
Related