c2-digit-year

Convert 2 digit year to 4 digit year


How would I convert a 2 digit year formatted like this into four year:

02 -> 2002
87 -> 1987

etc...

What I have so far:

char shortYr[3];
char longYr[5];
scanf("%2s", shortYr);
int shortYrAsInt = atoi(shortYr);

if (shortYrAsInt < 99)
    ;

How do I convert it? On the Internet, I read about converting 4 digit to 2 digit, which is easy, but what about the other way?


Solution

  • int longYear;
    if (shortYrAsInt <= 15) { // this should be the number where you think it stops to be 20xx (like 15 for 2015; for every number after that it will be 19xx)
        longYear = shortYrAsInt + 2000;
    } else {
        longYear = shortYrAsInt + 1900;
    }