none
C# DateTime Default Year Abbreviation RRS feed

  • Question

  • I'm currently using DateTime.TryParse to verify the string I am passed is valid.  What I would also like to do is if I'm passed a string like '01/02/99',  I would like the output DateTime parameter to read the value as '01/02/2099' and not '01/02/1999'.  Is there any native way where I can set the DateTime object to act that way.  Thanks.
    Saturday, November 5, 2011 5:40 AM

Answers

  • unfortunately there is no built in function to do this.you can use this function

     

     

          private string CorrectDateString(string InputDate)
            {
                string day;
                string tempday;
                string month;
                string year;
    
                month  = InputDate.Remove(2);
                tempday = InputDate.Remove(0,3);
                day = tempday.Remove(tempday.LastIndexOf("/"));
                year = InputDate.Remove(0, InputDate.LastIndexOf("/")+1);
    
                if (year.Length == 2)
                {
                    year = "20" + year;
                }
    
                return month + "/" + day + "/" + year;
            }
    


     

     


    Saturday, November 5, 2011 6:59 AM
  • Hi MarkHizzle,

    Another solution is The Calendar.TwoDigitYearMax property.

    Two-digit years are interpreted based on the value of the Calendar.TwoDigitYearMax property of the calendar used in the parsing operation. The Calendar.TwoDigitYearMax property defines the last year of a 100-year range that is used for interpreting two-digit years (From Interpreting Two-Digit Years in Community Content).

    Please try the code below:

    static void Main(string[] args)

    {

        string value = "01/02/99";

        Calendar cal = (Calendar)CultureInfo.CurrentCulture.Calendar.Clone();

        cal.TwoDigitYearMax = 2099;

        CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();

        culture.DateTimeFormat.Calendar = cal;

        Thread.CurrentThread.CurrentCulture = culture;

        DateTime dt = DateTime.Parse(value);

    }

     

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Louis.fr Monday, November 7, 2011 10:33 AM
    • Marked as answer by Bob Wu-MTModerator Wednesday, November 16, 2011 6:42 AM
    Monday, November 7, 2011 7:24 AM
    Moderator
  • Here a sample, it can be done of course with every method of DateTime to parse or construct it,

                DateTime dateTime = (new DateTime(1999, 02, 01)).AddYears(1000);
    

     


    Success
    Cor
    Saturday, November 5, 2011 12:09 PM
  • im no expert but maybe u can use the following concept as a reference

    to accomplish what u are trying to do

                StringBuilder sb = new StringBuilder(DateString);
                if (sb.Length == 8)
                {
                    sb.Insert(6, "20");            
                 }
    



    C# n00b coming through....
    Saturday, November 5, 2011 12:10 PM

All replies

  • unfortunately there is no built in function to do this.you can use this function

     

     

          private string CorrectDateString(string InputDate)
            {
                string day;
                string tempday;
                string month;
                string year;
    
                month  = InputDate.Remove(2);
                tempday = InputDate.Remove(0,3);
                day = tempday.Remove(tempday.LastIndexOf("/"));
                year = InputDate.Remove(0, InputDate.LastIndexOf("/")+1);
    
                if (year.Length == 2)
                {
                    year = "20" + year;
                }
    
                return month + "/" + day + "/" + year;
            }
    


     

     


    Saturday, November 5, 2011 6:59 AM
  • Here a sample, it can be done of course with every method of DateTime to parse or construct it,

                DateTime dateTime = (new DateTime(1999, 02, 01)).AddYears(1000);
    

     


    Success
    Cor
    Saturday, November 5, 2011 12:09 PM
  • im no expert but maybe u can use the following concept as a reference

    to accomplish what u are trying to do

                StringBuilder sb = new StringBuilder(DateString);
                if (sb.Length == 8)
                {
                    sb.Insert(6, "20");            
                 }
    



    C# n00b coming through....
    Saturday, November 5, 2011 12:10 PM
  • Hi MarkHizzle,

    Another solution is The Calendar.TwoDigitYearMax property.

    Two-digit years are interpreted based on the value of the Calendar.TwoDigitYearMax property of the calendar used in the parsing operation. The Calendar.TwoDigitYearMax property defines the last year of a 100-year range that is used for interpreting two-digit years (From Interpreting Two-Digit Years in Community Content).

    Please try the code below:

    static void Main(string[] args)

    {

        string value = "01/02/99";

        Calendar cal = (Calendar)CultureInfo.CurrentCulture.Calendar.Clone();

        cal.TwoDigitYearMax = 2099;

        CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();

        culture.DateTimeFormat.Calendar = cal;

        Thread.CurrentThread.CurrentCulture = culture;

        DateTime dt = DateTime.Parse(value);

    }

     

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Louis.fr Monday, November 7, 2011 10:33 AM
    • Marked as answer by Bob Wu-MTModerator Wednesday, November 16, 2011 6:42 AM
    Monday, November 7, 2011 7:24 AM
    Moderator

  • The calendar is just an extension to the methods of the DateTime Structure therefore that community content is in my idea incorrect.  

    The DateTime is done in a structure which has as base a long, which is represented the Gregorian Calendar and starts at 1/1/1 0:0:0 the start of the Gregorian Calendar. All representations are done by the methods and properties (in fact only Get methods) in the DateTime struct around that long which holds 100-nano seconds ticks since that date.

    By Adding that 1000 like in my sample as years, that 1000 simply is the value which is recalculated to the needed ticks for the DateTime.

    If the Tick property had also a Set property I could have showed

    DateTime.Tick += TheNumberOfTicksInThousandYear;

     


    Success
    Cor
    Monday, November 7, 2011 10:04 AM