JavaScriptでありえない日付をチェック
「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を返します。
[ 2023年1月12日 | カテゴリー: JavaScript | タグ: 日付 ]
« とにかく動かすPower Automate - その他編 | Pythonのturtleでコッホ曲線を描く方法 »
コメントを残す