Masriyah
Masriyah

Reputation: 2525

Expression edit

i am working on a report in asp.net, mvc/c# and in my report i want to display the date without the time so instead of

12/3/2011 00:00:00 ------ to ------ 12/3/2011

and i am using this expression for my text box that is displaying the date:

=FormatDateTime(Fields!DateClosed.Value)

and for an item with a date it does show properly without the time - but for other items in the list which do not have date they display the time like so: 12:00:00 AM

How can i fix the expression so that it will only show the date only when there is a date to display and not show the time when a date is not available.

Upvotes: 1

Views: 187

Answers (3)

Masriyah
Masriyah

Reputation: 2525

so i played around with this and i used a different format expression which lucky enough worked well. Thanks for everyone how helped.

=Format(Fields!DateClosed.Value, "d")

Upvotes: 1

3Dave
3Dave

Reputation: 29071

How about:

=(DateClosed==null) ? FormatDateTime(DateClosed) : string.Empty;

Also, you might use

=(DateClosed==null) ? DateClosed.Value.ToString("mm dd yyyy") : string.Empty;

instead of calling a separate FormatDateTime() function (unless, of course, you need to centralize date formatting, but that could be handled by storing the format in web.config).

Or even

string FormatDateTime(DateTime? date)
{
    var result = date==null ? date.ToString("mm dd yyyy") : string.Empty;
}

if you don't want to put the ternary conditional in the markup.

Upvotes: 2

Piotr Auguscik
Piotr Auguscik

Reputation: 3681

Check if the date is null and if it is then set string.Empty

Upvotes: 0

Related Questions