This is a migrated thread and some comments may be shown as answers.

dealing with nullable DateTime?

0 Answers 47 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Don
Top achievements
Rank 2
Don asked on 13 Sep 2013, 12:56 AM
Here is a nice little trick I found for dealing with data-type DateTime?

I was recently tasked with replacing a different venders UI toolset with yours and had a hard time dealing with nullable dates in existing code.
I finally created an extension method that allows me to quickly grab the text-field's value whether it has been set or not.
I hope this can save someone else time down the road.
          
public static string outputDate(this Telerik.Web.UI.RadDateTimePicker radPicker, string formatter)
{
    if (radPicker.IsEmpty || radPicker.SelectedDate == null) { return String.Empty; }
    switch (formatter.ToLower())
    {
        case "text":
        case "date": return radPicker.SelectedDate.Value.ToShortDateString();
        case "value":
        case "datetime": return radPicker.SelectedDate.Value.ToString();
        default:
            try {return radPicker.SelectedDate.Value.ToString(formatter); }
            catch {return String.Empty; }
    }           
}

No answers yet. Maybe you can help?

Tags
Calendar
Asked by
Don
Top achievements
Rank 2
Share this question
or