How do I replace the minimum values of a date with an empty string?

1

I have a program which calls a service when it executes, it gives me certain values and they are saved in a csv file. One of those values is a date which when it is empty shows me 1/1/0001 12:00:00 AM as a minimum value.

What I want is that before I show the result, I can replace this date with a null or void value so I do not have to show that date with values that can confuse the user.

This is part of my code:

newLine = string.Format("\"{0}\",\"{1}\"",


    item.InvitationDate, //Convert.ToDateTime(String.IsNullOrEmpty(item.InvitationDate) ? "" : 
    item.Reminder1SentDate);

As you can see, the item.invitationDate field is what results in what I indicated above. After the comment is what I tried to do but I have not been successful

Thanks

    
asked by A arancibia 27.10.2017 в 17:29
source

2 answers

1

Since you have a 1/1/0001 12:00:00 AM , that means you have the minimum value assigned to your variable of type DateTime .

To solve it, you should simply compare against that value, for example:

newLine = string.Format("\"{0}\",\"{1}\"",
item.InvitationDate, (item.InvitationDate == DateTime.MinValue ? string.Emtpy : item.InvitationDate.ToString()));
    
answered by 27.10.2017 / 17:57
source
1

The InvitationDate property must be nullable to to be able to achieve what you want. In your case, when you do not initialize a value the property InvitationDate , default value assigned is 1/1/0001 12:00:00 AM.

Convert the property to nullable so you can know if it is null, then show an empty space.

In the class where the property is defined, you define it as nullable like this:

public class TuClase
{
   //...
   public DateTime? InvitationDate {get;set;}
}

Then at the time of making the format would be like this:

newLine = string.Format("\"{0}\",\"{1}\"",
    item.InvitationDate.HasValue ? item.InvitationDate.ToString() : String.Empty, 
    item.Reminder1SentDate);
    
answered by 27.10.2017 в 17:46