1.示例:条件有效
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.示例:条件有效
date_from = "2014-05-21 16:00:00";
date_now = "2014-05-21 18:00:00";
date_to = null;
3.示例:条件有效
date_from = null;
date_now = "2014-05-21 18:00:00";
date_to = "2014-05-21 19:00:00";
4.示例:条件无效
date_from = "2014-05-21 16:00:00";
date_now = "2014-05-21 18:00:00";
date_to = "2014-05-21 17:00:00";
我有这个条件:
if (data1.date_from <= voting_started && voting_started <= data1.date_to) {
//abc
}
如果date_from和date_to都不为null,那么它运行良好,但如果一个为null,则条件无效。如何用空值检查日期?
答案 0 :(得分:2)
将其标准化。
之前您的if
添加此内容:
if(!date_to) {
date_to = voting_started;
};
if(!date_from) {
date_from = voting_started;
}
答案 1 :(得分:1)
或:
if ((data1.date_from === null || data1.date_from <= voting_started) &&
(data1.date_to === null || voting_started <= data1.date_to)) {
//abc
}