How to Check If a Datetime Value Is Empty or Not in a Put Request

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 for Integer, False for Boolean, DateTime.MinValue for DateTime, ...
  • 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 equivalently d = 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:

  1. undefined
  2. null
  3. false
  4. ""
  5. 0 and -0
  6. 0n
  7. 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



Leave a reply



Submit