「2月30日」はありえない日付です。
このようなありえない日付をJavaScriptでチェックする方法です。
m = 2;
d = 30;
result = is_date(m, d);
console.log(result);
function is_date(m, d){
obj = new Date(2020, m - 1, d);
this_month = obj.getMonth() + 1;
this_day = obj.getDate();
return this_month == m && this_day == d;
}
d = 30;
result = is_date(m, d);
console.log(result);
function is_date(m, d){
obj = new Date(2020, m - 1, d);
this_month = obj.getMonth() + 1;
this_day = obj.getDate();
return this_month == m && this_day == d;
}
is_dateという関数に月と日を与えます。
ありえる日付ならばtrue、ありえない日付ならばfalseを返します。
コメント