dealing with nullable DateTime?

1 posts, 0 answers
  1. Don
    Don avatar
    38 posts
    Member since:
    Oct 2006

    Posted 12 Sep 2013 Link to this post

    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; }
        }           
    }
Back to Top