Convert a two digit year to a four digit year

前端 未结 17 985
误落风尘
误落风尘 2020-12-25 12:13

This is a question of best practices. I have a utility that takes in a two digit year as a string and I need to convert it to a four digit year as a string. right now I do <

17条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 12:57

    This Method can convert the credit card last two year digits to four year


    private static int ToFourDigitYear(int year)
    {
        string stringYear = year.ToString("00");
        if (stringYear.Length == 2)
        {
            int currentYear = DateTime.Now.Year;
            string firstTwoDigitsOfCurrentYear = currentYear.ToString().Substring(0, 2);
            year = Convert.ToInt32(firstTwoDigitsOfCurrentYear + stringYear);
            if (year < currentYear)
                year = year + 100;
        }
        return year;
    }
    

提交回复
热议问题