可能重复:
JavaScript 日期对象英国日期
我有以下代码,我在将日期格式设置为英国时遇到问题,即
var birthday = new Date("20/9/1988");
当我运行我得到的代码时You are NaN years old
。错误但如果我将其更改为 09/20.1988 它可以工作
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.