Search code examples
pythondatetimeintegernumber-formattingdata-conversion

How to convert numbers to dates


the emplyee number is composed of year and month and 3 digit control number how to know the number of years they works if we base on todays date? Employee1 201011003, eployee2 200605015


Solution

  • You can use datetime library like this:

    from datetime import date
    
    date_str = '201011003'
    year = int(date_str[0:4])
    month = int(date_str[4:6])
    d = date(year, month, 1)
    
    year_delta = (date.today() - d).days // 365
    print(year_delta)