Current Date Time

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

2 questions...

The documentation shows a "property" call Now, which returns the current
date and time, but it is not valid for how I am using it.

If you declare a field as a DateTime, but never populate it, how can you
tell if it is null?
 
Jim:

What do you need to do with it? For instance, DateTime.Now has a lot of
formatters and can do just most anything I've ever come across.

I don't have my IDE in front of me, but if you don't assign the object a
value, it should be null in C# or nothing in VB.NET. If may have some
other value, but I would think it'd be null. You can declare a DateTime in
the IDE and breakpoint before it. Step past it and see what it's value is.
You can check for whatever that is afterward.

HTH,

Bill
 
Jim:

I just remembered something...DateTime is a value type, nix that last
suggestion. Let me play with it for a second and I'll post back.
 
Hi,

DateTime DoStuff()
{
DateTime dateTime = DateTime.MinValue;

// Do other stuff.

return(dateTime);
}

void DoMoreStuff()
{
DateTime dateTime = DoStuff();

if(dateTime == DateTime.MinValue)
{
// invalid date.
}
}

John.
 
2 questions...

The documentation shows a "property" call Now, which returns the current
date and time, but it is not valid for how I am using it.

If you declare a field as a DateTime, but never populate it, how can you
tell if it is null?

DateTime object are value types, they are never null, in the same way
that ints and longs can never be null.

If you declare a local variable as DateTime, the compiler won't let you
use it until you populate it. If you declare a member variable as a
DateTime, it will be equal to DateTime.MinValue.

And I'm not really sure what your first question refers to. How do you
want to use it?
 
Jim,
In addition to the others comments about using DateTime.MinValue.

You might consider using System.Data.SqlTypes.SqlDateTime, as it is a value
type that has the concept of being "Nullable".

Hope this helps
Jay
 
Back
Top