locked
Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globalization.CultureInfo.InvariantCulture.Calendar; DateTime fileDate = new DateTime(15, 2,25 , dteCal); RRS feed

  • Question

  • User-1668220527 posted

    Hi folks

    Simple problem, I have a two digit year input which i want to be interpreted as a four digit year so i assumed the following date time constructor would do the trick:

    System.Globalization.Calendar dteCal=System.Globalization.CultureInfo.InvariantCulture.Calendar;
    
    DateTime fileDate = new DateTime(15, 2,25 , dteCal);

    This is because dteCal.TwoDigitYearMax=2029 so i would expect fileDate to be 25/2/2015 (UK date).  However, it is 25/02/0015.

    Now I have got round this by int fileYear=dteCal.ToFourDigitYear(int.Parse(lstItems[0].Substring(6,2))) and using the constructor without the calendar object but surely there must be a way to create a DateTime object when you only have a two digit year?

    Thursday, February 26, 2015 9:11 AM

Answers

  • User281315223 posted

    Since DateTime objects can technically support dates as low as the year 15 (e.g. 0015), you aren't going to be able to use this particular constructor by passing in 15 (as .NET will associate that to the year 15).

    You may want to consider actually parsing the Date instead in the format that you are expecting :

    // Define your day, month, year here
    int day = 25;
    int month = 2;
    int year = 15;
    
    // Parse your date as expected
    DateTime fileDate = DateTime.ParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", null);

    Or you could make this into your own function as follows :

    public DateTime DateTimeWithTwoDigitYear(int year, int month, int day)
    {
          DateTime dt = DateTime.MinValue;
    
          DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", DateTimeStyles.None, null, out dt);
    
          return dt;
    }

    You can see a working example of this here.

    DateTime objects do not have any formatting on their own, so whenever you have a particular format that you want to use to output a DateTime object, you'll need to explicitly define the format.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 26, 2015 9:35 AM

All replies

  • User1577371250 posted

    Hi,

    I guess you need to use the Calendar to get the 4 digit Year from 2 digit Year. If you want the other way you have to write a function and manually do that how the .NET Framework has done internally for Calendar.

    https://msdn.microsoft.com/en-us/library/system.globalization.calendar.tofourdigityear%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    System.Globalization.Calendar dteCal = System.Globalization.CultureInfo.InvariantCulture.Calendar;
    int year = dteCal.ToFourDigitYear(15);
    DateTime fileDate = new DateTime(year, 2, 25);

    Thursday, February 26, 2015 9:27 AM
  • User281315223 posted

    Since DateTime objects can technically support dates as low as the year 15 (e.g. 0015), you aren't going to be able to use this particular constructor by passing in 15 (as .NET will associate that to the year 15).

    You may want to consider actually parsing the Date instead in the format that you are expecting :

    // Define your day, month, year here
    int day = 25;
    int month = 2;
    int year = 15;
    
    // Parse your date as expected
    DateTime fileDate = DateTime.ParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", null);

    Or you could make this into your own function as follows :

    public DateTime DateTimeWithTwoDigitYear(int year, int month, int day)
    {
          DateTime dt = DateTime.MinValue;
    
          DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", DateTimeStyles.None, null, out dt);
    
          return dt;
    }

    You can see a working example of this here.

    DateTime objects do not have any formatting on their own, so whenever you have a particular format that you want to use to output a DateTime object, you'll need to explicitly define the format.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 26, 2015 9:35 AM
  • User-434868552 posted

    @mmacneill123

    NEVER use TWO DIGIT years.

    once upon a time there were PUNCHED CARDS*.

    Foolishly, trying to make to most out of 80 columns, many application designers used ONLY the last two digits of the year.

    The cost of this folly was millions and millons and millions of U.S. dollars.  http://en.wikipedia.org/wiki/Year_2000_problem#Cost 

    USE four digit years.

    Using two digit years saves you nothing in memory because ALL dates that use the DateTime struct are the same size.  https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx 

    N.B.:  if all of your dates are 20nn make your text box like this:

                   Year (21nn) [           ]  00-99   last two digits only.

    The add 2000 server side.

    http://upload.wikimedia.org/wikipedia/commons/4/4c/Blue-punch-card-front-horiz.png:

    80 column punched card

    Thursday, February 26, 2015 10:03 AM
  • User-1668220527 posted

    Thanks (diolch) Rion

    Almost:

    DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", DateTimeStyles.None, null, out dt);
    
    should be:
    
    DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}", day, month, year), "dd/MM/yy",null, DateTimeStyles.None,  out dt);

    Appreciate the insights!

    Thursday, February 26, 2015 10:17 AM
  • User-1668220527 posted

    NEVER use TWO DIGIT years

    Yes, totally agree.  Unfortunately the Azure FTP directory listing returns the file date years as two digits.  Yes ironic :-)

    Thursday, February 26, 2015 10:20 AM
  • User-434868552 posted

    @mmacneill123

    would you mind explaining why you would not simply use this constructor with a 4-digit year?

    https://msdn.microsoft.com/en-us/library/xcfzdy4x(v=vs.110).aspx 

         "DateTime Constructor (Int32, Int32, Int32)"

    Parameters

    year
    Type: System.Int32

    The year (1 through 9999).

    month
    Type: System.Int32

    The month (1 through 12).

    day
    Type: System.Int32

    The day (1 through the number of days in month).

    Thursday, February 26, 2015 10:26 AM
  • User-1910946339 posted

    the Azure FTP directory listing returns the file date years as two digits
    Can you show the code that does this?

    Wednesday, March 4, 2015 11:03 PM