Convert a two digit year to a four digit year

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

    Had a similar issue, and came up with this... HTH!

    value = this.GetDate()
    
    if (value.Length >= 6)//ensure that the date is mmddyy
    {
        int year = 0;
    
        if (int.TryParse(value.Substring(4, 2), out year))
        {
            int pastMillenium = int.Parse(DateTime.Now.ToString("yyyy").Substring(0, 2)) - 1;
    
            if (year > int.Parse(DateTime.Now.ToString("yy")))//if its a future year it's most likely 19XX
            {
                value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium, year.ToString().PadLeft(2, '0'));
            }
            else
            {
                value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium + 1, year.ToString().PadLeft(2, '0'));
            }
        }
        else
        {
            value = string.Empty;
        }
    }
    else
    {
        value = string.Empty;
    }
    
    0 讨论(0)
  • 2020-12-25 12:46

    You can also use the DateTime.TryParse method to convert your date. It uses the current culture settings to define the pivot year (in my case it is 2029)

    DateTime resultDate;
    Console.WriteLine("CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax : {0}", System.Globalization.CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax);
    DateTime.TryParse("01/01/28", out resultDate);
    Console.WriteLine("Generated date with year=28 - {0}",resultDate);
    DateTime.TryParse("01/02/29",out resultDate);
    Console.WriteLine("Generated date with year=29 - {0}", resultDate);
    DateTime.TryParse("01/03/30", out resultDate);
    Console.WriteLine("Generated date with year=30 - {0}", resultDate);
    

    The output is:

    CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax : 2029

    Generated date with year=28 - 01/01/2028 00:00:00

    Generated date with year=29 - 01/02/2029 00:00:00

    Generated date with year=30 - 01/03/1930 00:00:00

    If you want to change the behavior you can create a culture with the year you want to use as pivot. This thread shows an example

    DateTime.TryParse century control C#

    But as martin stated, if you want to manage a time period that spans more than 100 year, there is no way to do it with only 2 digits.

    0 讨论(0)
  • 2020-12-25 12:46

    Out of curiosity, from where do you get this data? From a form? In that case; I would simply ask the user to fill in (or somehow select) the year with four digits or get the users age and month/day of birth, and use that data to figure out what year they were born. That way, you wouldn't have to worry about this problem at all :)

    Edit: Use DateTime for working with this kind of data.

    0 讨论(0)
  • 2020-12-25 12:46

    My two cents,

    Given an age range=[18, 100+], two digits year=90, I can do

    current year - twoDigitsYear = 2018 - 90 = 1928, I got 19, 28

    hence 19 is the first two digits of year of born, and 28 is the age, which is
    year=1990, age=28

    But it won't work when age 0 and 100 both included in the range, same to some of the other answers here.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-25 12:49

    Try this simple code

    //Invoke TextBoxDateFormat method with date as parameter.

    Method

    public void TextBoxDateFormat(string str1)
    
    {
    
    // Takes the current date format if MM/DD/YY or MM/DD/YYYY
    
    DateTime dt = Convert.ToDateTime(str1);
    
    //Converts the requested date into MM/DD/YYYY and assign it to textbox field
    
    TextBox = String.Format("{0:MM/dd/yyyy}", dt.ToShortDateString());
    
    
    //include your validation code if required
    
    }
    
    0 讨论(0)
提交回复
热议问题