Solved

How to return the time portion of a date/time field when the field is nullable

Posted on 2013-05-29
2
463 Views
Last Modified: 2013-05-30
I have a class that stores the start and end times of a shift.  I want the field to be able to hold a null value.  Here is how I originally had the field defined:

private DateTime _MonStartTime;
        public String MonStartTime
        {
            get
            {
                return this._MonStartTime.ToShortTimeString();
            }
            set
            {
                this._MonStartTime = Convert.ToDateTime(value);
            }

        }

Open in new window


So basically I want to store the value as a Date/Time but display the value to the user as Time only in a String variable.

Using the above definition, MonStartTime cannot be null.  If I change the definition to the following, MonStartTime can be null but I can't figure out how to display the Time only to the user if the value is not null.  

        private DateTime? _MonStartTime;        // ? allows Date to be nullable
        public String MonStartTime
        {
            get
            {
                return this._MonStartTime.ToString();
            }
            set
            {
                if (value != null)
                    this._MonStartTime = Convert.ToDateTime(value);
                else
                    this._MonStartTime = null;
            }

        }

Open in new window


I get an error if I try and use .ToShortTimeString();


System.Nullable<System.DateTime>' does not contain a definition for 'ToShortTimeString' and no extension method 'ToShortTimeString' accepting a first argument of type 'System.Nullable<System.DateTime>' could be found (are you missing a using directive or an assembly reference?)

Any suggestions are greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 39206244
You just need to define NullDisplayText  (="" or whatever) as a constant in your class.
then check if the prop is null  as follows:
dt2 != null ? dt2.Value.ToString("yyyy-MM-dd hh:mm:ss") : NullDisplayText;

Open in new window

where dt2 is your property to check.
Check:
http://stackoverflow.com/questions/1833054/how-can-i-format-a-nullable-datetime-with-tostring
0
 

Author Closing Comment

by:dyarosh
ID: 39207479
I was able to use this._MonStartTime.Value.ToShortTimeString();

Thanks for your help!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question