Solved

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

Posted on 2013-05-29
2
417 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 35

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now