Convert a two digit year to a four digit year

前端 未结 17 971
误落风尘
误落风尘 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:47

    The implementation of

    System.Globalization.CultureInfo.CurrentCulture.Calendar.ToFourDigitYear 
    

    is

    public virtual int ToFourDigitYear(int year)
    {
      if (year < 0)
        throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
      if (year < 100)
        return (this.TwoDigitYearMax / 100 - (year > this.TwoDigitYearMax % 100 ? 1 : 0)) * 100 + year;
      else
        return year;
    }
    

    Hope this helps!

提交回复
热议问题