How to check if a DateTime field is not null or empty?
If you declare a DateTime, then the default value is DateTime.MinValue, and hence you have to check it like this:
DateTime dat = new DateTime();
if (dat==DateTime.MinValue)
{
//unassigned
}
If the DateTime is nullable, well that's a different story:
DateTime? dat = null;
if (!dat.HasValue)
{
//unassigned
}
Checking to see if a DateTime variable has had a value assigned
The only way of having a variable which hasn't been assigned a value in C# is for it to be a local variable - in which case at compile-time you can tell that it isn't definitely assigned by trying to read from it :)
I suspect you really want Nullable<DateTime>
(or DateTime?
with the C# syntactic sugar) - make it null
to start with and then assign a normal DateTime
value (which will be converted appropriately). Then you can just compare with null
(or use the HasValue
property) to see whether a "real" value has been set.
DateTime value NULL when UI culture is Auto
Parsing the date to 'yyyy-mm-dd' in client side javascript, before posting the data worked.
Used moment.js for the same.
Why can't I check if a 'DateTime' is 'Nothing'?
This is one of the biggest sources of confusion with VB.Net, IMO.
Nothing
in VB.Net is the equivalent of default(T)
in C#: the default value for the given type.
- For value types, this is essentially the equivalent of 'zero':
0
forInteger
,False
forBoolean
,DateTime.MinValue
forDateTime
, ... - For reference types, it is the
null
value (a reference that refers to, well, nothing).
The statement d Is Nothing
is therefore equivalent to d Is DateTime.MinValue
, which obviously does not compile.
Solutions: as others have said
- Either use
DateTime?
(i.e.Nullable(Of DateTime)
). This is my preferred solution. - Or use
d = DateTime.MinValue
or equivalentlyd = Nothing
In the context of the original code, you could use:
Dim d As DateTime? = Nothing
Dim boolNotSet As Boolean = d.HasValue
A more comprehensive explanation can be found on Anthony D. Green's blog
Flutter How to validate DateTime isn't null
validator: (value) {
if (value.isEmpty)
return "Please pick a date";
}
This is assuming that you did not give an initial value to your dateController.
If you did give it an initial value, for example,
dateController.text = "Please pick a date"
if (dateController.text == "Please pick a date")
return "Please pick a date";
This will check if the value of the controller has changed from the initial value or not. If it didn't change, it'll show the text being returned in the if statement. When your user picks a date, you are change the value of it to the date picked, so it'll pass the validation, otherwise, it'll fail.
How to check the (empty) value of a input type="date" in Chrome
You could check for a falsy value:
if (!valueDate) {
// ...
}
The falsy values in JavaScript are:
undefined
null
false
""
0
and-0
0n
NaN
Since document.getElementById('Date').value
is always of type string if a value is set, you don't get false positives like 0
being treated like no input, which would be the case if the type was number
.
DateTime "null" value
For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue
, because it is a value type rather than a reference type.
You can also use a nullable DateTime, like this:
DateTime? MyNullableDate;
Or the longer form:
Nullable<DateTime> MyNullableDate;
And, finally, there's a built in way to reference the default of any type. This returns null
for reference types, but for our DateTime example it will return the same as DateTime.MinValue
:
default(DateTime)
or, in more recent versions of C#,
default
Related Topics
Return Json, But It Includes Backward Slashes "\", Which I Don't Want
How to Get User Browser Name ( User-Agent ) in ASP.NET Core
Comparing Two Lists and Return Not Matching Items Results With Error
How to Get the Currently Loggedin Windows Account from an ASP.NET Page
Convert a List of Objects from One Type to Another Using Lambda Expression
How to Loop Through All Fields in an Object in C#
Use Linq to Collaps Multiply Collections into One Collection
Browser Back Button Navigates to Login Page When User Is Still Logged In
Can Newtonsoft Json.Net Skip Serializing Empty Lists
How to Automatically Increment Numbers in C#
How to Call Another Controller Action from a Controller in MVC
C# Check the Empty or Null Value in All Datatable Values
String to Decimal With 2 Decimal Places Always
How to Generate a System (Pc/Laptop) Hardware Unique Id in C#
How to Remove Carriage Returns from the Json Output of My Webapi Service
How to Get the Display Name Attribute of an Enum Member Via MVC Razor Code
How to Get Cookies Info Inside of a Cookiecontainer (All of Them, Not for a Specific Domain)