Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net Errors when sending data to SQL 5 21
ASP.NET MVC -Add authentication 2 29
insert value of checklistbox checked 4 32
asp.net web application 3 26
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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