Date ONLY data type.




I'd like to be able to distinguish between a DateTime variable that is
allowed to hold both date and time and a DateTime variable that is allowed
to only hold a Date.
The reason's for this is that I'd like to store DateTime variables that are
allowed to hold date and time as UTC in the database.
However I'd to store DateTime variables that are only allowed to hold a date
(like a birthdate) not as UTC (since adding a +6h time offset to something
that didn't have a time specified in the first place isn't meaningful).
Unfortunately, I DO have scenarios where I have date and time combinations
where 00:00 is a valid entered time (otherwise I could have used that to
determine if it was a date only).

For example I'd like to be able to do something like this:

public static void AddParameter(ref SqlCommand aCommand, string aName,
DateTime aValue)
if (!aValue.ISDATEONLY) //<-- ISDATEONLY isn't a method that exists,
I'm looking for something like that.
// The value needs to be save in the database in UTC
aValue = aValue.ToUniversalTime();
AddParameter(ref aCommand, aName, aValue);

Now I figured I'd define a class something like this in order to indicate if
a variable is a date only ( I'm used to have something similar available in
Delphi ) :

public class Date : DateTime

However, DateTime is sealed. :(

Now I could do something like this:

public class Date
DateTime Value;

However, I'd like to be able to bind properties defined as Date to UI Date /
Time controls, so I'd like to keep working with DateTime. (Or is there an
easy way to bind something like the Date class above to a UI Date / Time



Ignacio Machin \( .NET/ C# MVP \)


I think your solution is good, create a class with a member of DateTime
and a bool that indicate if it hold a Date only.

You could also add other members as needed.

You could use then MyDate.TheDateTime to bind to the datetimepick control


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Edit only date in grid 2
Date server webcontrol 1
How to instantiate a particular TimeZone? 2
Extend Types like instances? 8
C# help!!! Convert to VB 2
CountDown Timer 4
Injecting code into linq 16
Convert Date 2