重複の可能性:
JavaScript 日付オブジェクトの英国の日付
次のコードがあり、日付形式を英国式に設定する際に問題が発生しています。
var birthday = new Date("20/9/1988");
コードを実行すると、You are NaN years old
. エラーですが、1988 年 9 月 20 日と変更すると動作します
var birthday = new Date("20/9/1988");
var today = new Date();
var years = today.getFullYear() - birthday.getFullYear();
// Reset birthday to the current year.
birthday.setFullYear(today.getFullYear());
// If the user's birthday has not occurred yet this year, subtract 1.
if (today < birthday)
{
years--;
}
document.write("You are " + years + " years old.");
// Output: You are years years old.