0

Im showing Warrenty_Expires of product in my view , but sometimes Warrenty_Expires contains value exactly like this 1753-01-01 00:00:00.000 and i want to say if Warrenty_Expires contains this value should not display it in my view.Can anyone please help me or point me into the right direction :)

This is i end up with but its not working and still showing this value (1753-01-01).

View:

  if (Field.WarrentyExpiress.HasValue) //Check for null value
 {
          //Check if Warrenty contains this value(1753-01-01)

         if (Field.WarrentyExpiress.Value.ToString().Contains("1753-01-01"))
          {
           <td>Not available</td>

          }

         else
          {
             <td>@Field.WarrentyExpiress.Value.ToString("MM/dd/yyyy")</td>

          }

 }

else
 {
   <td>Not available</td>
 }

Model:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime Warrenty_Expires { get; set; }

ViewModel:

public DateTime? WarrentyExpiress { get; set; }
The First
  • 139
  • 2
  • 13
  • If specified year, month and day is bothering you, I think you can just check DateTime.Year, DateTime.Month and DateTime.Day – SlaneR Nov 15 '18 at 09:02
  • 2
    `If (Field.WarrentyExpiress.Value == new DateTime(1753, 1, 1))` –  Nov 15 '18 at 09:02

4 Answers4

0

Don't mess around with string representations of DateTime, simply compare against another instance of the DateTime struct:

if (Field.WarrentyExpiress.Value == new DateTime(1753, 1, 1).Date)
{
    <td>Not available</td>
}

BTW, an interesting read: Why 1753?

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

What if you want to use multilingual? I would use the Date property of the DateTime.

if (Field.WarrentyExpiress.Value.Date == new DateTime(1753,1,1))
{
    ....
}

Never compare ToString() when something is regional dependend.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

It's very unlikely that the year 1753 would be anything but this special case, so you could even do:

if (Field.WarrentyExpiress.Value.Year == 1753)
Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
0

Do not try to do this via string processing. String is just about the 2nd worst format you could work with.

Figure out the actuall value of DateTime.Ticks that 1753-01-01 00:00:00.000 represents. DateTime just stores the amount of ticks since 1st January 0001 00:00:00.000. Every other value - the day, the month, the year, the string representation - are just a interpretation of that Tick value.

Once you know what the tick count is, you can check for anything less or equal to that count and filter it out.

Christopher
  • 9,634
  • 2
  • 17
  • 31