?
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
?
479 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
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

807 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