user3357400
user3357400

Reputation: 397

How to check date to/from with null value

1.Example: condition is valid

date_from = "2014-05-21 16:00:00";
date_now  = "2014-05-21 18:00:00";
date_to   = "2014-05-21 19:00:00";

2.Example: condition is valid

date_from = "2014-05-21 16:00:00";
date_now  = "2014-05-21 18:00:00";
date_to   = null;

3.Example: condition is valid

date_from = null;
date_now  = "2014-05-21 18:00:00";
date_to   = "2014-05-21 19:00:00";

4.Example: condition is not valid

date_from = "2014-05-21 16:00:00";
date_now  = "2014-05-21 18:00:00";
date_to   = "2014-05-21 17:00:00";

I have this condition:

if (data1.date_from <= voting_started && voting_started <= data1.date_to) {
//abc
} 

It works well, if both date_from and date_to are not null, but if one is null, condition is not valid. How to check date with null value?

Upvotes: 0

Views: 61

Answers (2)

Maurice Perry
Maurice Perry

Reputation: 32831

or:

if ((data1.date_from === null || data1.date_from <= voting_started) &&
    (data1.date_to === null || voting_started <= data1.date_to)) {
//abc
}

Upvotes: 1

iamruss
iamruss

Reputation: 812

Normalize it.

Before your if add this:

if(!date_to) {
    date_to = voting_started;
};
if(!date_from) {
    date_from = voting_started; 
}

Upvotes: 2

Related Questions