?
Solved

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

Posted on 2013-05-29
2
Medium Priority
?
469 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

764 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