Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Comparison between dates in C# application

I have an asp.net application in which i have these three dates:

now = 08-10-13 15:56:19

cloture1 = 01-10-13 00:00:00

cloture2= 01-01-50 00:00:00

The format of dates is DD-MM-YY HH:MM:SS. the problem is that the function DateTime.Compare() gives me the same result ie

DateTime.Compare(now,cloture1) > 0 and DateTime.Compare(now,cloture2) > 0.

So what is the reasons of this problem? How can i fix my snippet?

Upvotes: 0

Views: 454

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98740

From The "yy" Custom Format Specifier

In a parsing operation, a two-digit year that is parsed using the "yy" custom format specifier is interpreted based on the Calendar.TwoDigitYearMax property of the format provider's current calendar.

In a parsing operation, a two-digit year that is parsed using the "yy" custom format specifier is interpreted based on the Calendar.TwoDigitYearMax property of the format provider's current calendar. The following example parses the string representation of a date that has a two-digit year by using the default Gregorian calendar of the en-US culture.

From GregorianCalendar.TwoDigitYearMax

This property allows a 2-digit year to be properly translated to a 4-digit year. For example, if this property is set to 2029, the 100-year range is from 1930 to 2029. Therefore, a 2-digit value of 30 is interpreted as 1930, while a 2-digit value of 29 is interpreted as 2029.

Your application should set this value to 99 to indicate that 2-digit years are to be taken literally. For example, if this property is set to 99, the 100-year range is from 0 (not a valid value for most calendars) to 99. Therefore, a 2-digit value of 30 is interpreted as 30.

Even when you decompile GregorianCalendar.TwoDigitYearMax property, you can see yourself;

public override int TwoDigitYearMax
{
  get
  {
    if (this.twoDigitYearMax == -1)
      this.twoDigitYearMax = Calendar.GetSystemTwoDigitYearSetting(this.ID, 2029);
    return this.twoDigitYearMax;
  }

Boluc's answer is completely right. I want to point also your format part.

You can't format two digit year with YYYY format. You need to use yy format which allows two digit formatting.

DateTime dt = DateTime.ParseExact("01-01-50 00:00:00", "dd-MM-yy HH:mm:ss", CultureInfo.InvariantCulture);

Prints

01.01.1950 00:00:00

Here a DEMO.

Check out for more informations from Custom Date and Time Format Strings

Upvotes: 1

Boluc Papuccuoglu
Boluc Papuccuoglu

Reputation: 2346

The problem is your program most probably interpreting cloture2 as 1950, not 2050. Because you have not posted the code in which you set cloture2, I cannot offer a concrete solution, but the best I can offer is that you use 01-10-2050 explicitly in your code.

Upvotes: 6

Nunners
Nunners

Reputation: 3047

If now is later than cloture1 then the returned value will be 1 (or Greater than Zero).

The code you have supplied along with the example dates seems to work fine in reference to the MSDN article for DateTime.Compare method.

Please check the following link for more information on the DateTime.Compare method:

http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx

If you believe that your code is still incorrect please elaborate on your question.

Upvotes: 1

Related Questions