Javascript returns NaN 仅在 IOS 浏览器中
Javascript returns NaN only in IOS browsers
我正在制作这个年龄计算器网站,它使用 JS 中的 Date 对象,并通过减去当前时间(以毫秒为单位)和用户的出生日期(以毫秒为单位)来计算不同单位(年、月、日等)的年龄并将其输出到 html.
它在 windows 和 android 上完全可用,但是,当网站在 IOS 中加载时它 returns NaN。我不知道它是否与 parseInt() 函数有关,或者在这两个平台中自动完成的事情在 IOS 中没有完成。参考网址:https://hesamzakeri.ir/en/
<div id="years"></div>
<div id="months"></div>
<div id="days"></div>
<div id="hours"></div>
<div id="minutes"></div>
<div id="seconds"></div>
<script>
let currentTime = Date.now();
let birthday = new Date(2002 + '-' + 07 + '-' + 04); // example
let birthday = Date.parse(birthday);
let age = currentTime - birthday;
let yearsOld = parseInt(age / 1000 / 60 / 60 / 24 / 365, 10);
let yearsOldInMs = yearsOld * 365 * 24 * 60 * 60 * 1000;
let monthsOld = parseInt((age - yearsOldInMs) / 1000 / 60 / 60 / 24 / 30.417, 10);
let monthsOldInMs = monthsOld * 30.417 * 24 * 60 * 60 * 1000;
let daysOld = parseInt((age - (yearsOldInMs + monthsOldInMs)) / 1000 / 60 / 60 / 24, 10);
let daysOldInMs = daysOld * 24 * 60 * 60 * 1000;
let hoursOld = parseInt((age - (yearsOldInMs + monthsOldInMs + daysOldInMs)) / 1000 / 60 / 60, 10);
let hoursOldInMs = hoursOld * 60 * 60 * 1000;
let minutesOld = parseInt((age - (yearsOldInMs + monthsOldInMs + daysOldInMs + hoursOldInMs)) /
1000 /
60,
10
);
let minutesOldInMs = minutesOld * 60 * 1000;
let secondsOld = parseInt(
(age -
(yearsOldInMs +
monthsOldInMs +
daysOldInMs +
hoursOldInMs +
minutesOldInMs)) /
1000,
10
);
</script>
这是它计算年龄的部分之一。我省略了 JS 中不必要的部分,例如它将结果输出到 HTML.
您正在将 Date 对象传递到 Date.parse(),这需要一个字符串。
您的代码:
birthday = new Date(formYear + '-' + formMonth + '-' + formDay);
birthday = Date.parse(birthday); // NaN
将最后一行更改为:
birthday = Date.parse(birthday.toString());
更新:
OP 评论说问题出在 new Date()
的非标准日期字符串输入上,而不是对 Date.parse()
.
的调用
日期字符串输入已修复,但对 Date.parse()
的调用仍然错误(尽管某些浏览器可能仍然可以使用它)。
// this is correct
birthday = new Date(2001, 2, 4);
console.log(typeof birthday); // object
// this is NOT correct, argument to Date.parse is type 'object'
// (Date.parse argument must be type 'string')
birthday = Date.parse(birthday);
// this is what you want:
let birthTime = birthday.getTime();
Date.parse 需要一个字符串参数,来自 MDN:
dateString
A string representing a simplification of the ISO 8601 calendar date extended format. (Other formats may be used, but results are implementation-dependent.)
即使将有效的日期字符串传递给 Date.parse,您也应该避免完全使用 Date.parse,因为不同的主机解析日期字符串的方式不同。来自 MDN:
It is not recommended to use Date.parse
as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).
您的代码中的问题是当您使用 2002 + '-' + 07 + '-' + 04
时有两个问题,首先,当您定义一个带有前导零 (0) 的数字时,它认为该数字是一个八进制整数,因此您可以喜欢 09
或 08
。
其次,如果你计算 2002 + '-' + 07 + '-' + 04
它将是 2002-7-4
,这不是 JS 规范文档的有效日期,Chrome 和 Firefox 足够聪明,可以将这个字符串解析为日期值,但 Safari 认为这是一个无效值。
你可以这样做:
let birthday = new Date(2002,7,4);
此外,您定义了两次 birthday
,因此只需定义一次。
let birthday = new Date(2002,7,4); // example
birthday = Date.parse(birthday);
我正在制作这个年龄计算器网站,它使用 JS 中的 Date 对象,并通过减去当前时间(以毫秒为单位)和用户的出生日期(以毫秒为单位)来计算不同单位(年、月、日等)的年龄并将其输出到 html.
它在 windows 和 android 上完全可用,但是,当网站在 IOS 中加载时它 returns NaN。我不知道它是否与 parseInt() 函数有关,或者在这两个平台中自动完成的事情在 IOS 中没有完成。参考网址:https://hesamzakeri.ir/en/
<div id="years"></div>
<div id="months"></div>
<div id="days"></div>
<div id="hours"></div>
<div id="minutes"></div>
<div id="seconds"></div>
<script>
let currentTime = Date.now();
let birthday = new Date(2002 + '-' + 07 + '-' + 04); // example
let birthday = Date.parse(birthday);
let age = currentTime - birthday;
let yearsOld = parseInt(age / 1000 / 60 / 60 / 24 / 365, 10);
let yearsOldInMs = yearsOld * 365 * 24 * 60 * 60 * 1000;
let monthsOld = parseInt((age - yearsOldInMs) / 1000 / 60 / 60 / 24 / 30.417, 10);
let monthsOldInMs = monthsOld * 30.417 * 24 * 60 * 60 * 1000;
let daysOld = parseInt((age - (yearsOldInMs + monthsOldInMs)) / 1000 / 60 / 60 / 24, 10);
let daysOldInMs = daysOld * 24 * 60 * 60 * 1000;
let hoursOld = parseInt((age - (yearsOldInMs + monthsOldInMs + daysOldInMs)) / 1000 / 60 / 60, 10);
let hoursOldInMs = hoursOld * 60 * 60 * 1000;
let minutesOld = parseInt((age - (yearsOldInMs + monthsOldInMs + daysOldInMs + hoursOldInMs)) /
1000 /
60,
10
);
let minutesOldInMs = minutesOld * 60 * 1000;
let secondsOld = parseInt(
(age -
(yearsOldInMs +
monthsOldInMs +
daysOldInMs +
hoursOldInMs +
minutesOldInMs)) /
1000,
10
);
</script>
这是它计算年龄的部分之一。我省略了 JS 中不必要的部分,例如它将结果输出到 HTML.
您正在将 Date 对象传递到 Date.parse(),这需要一个字符串。
您的代码:
birthday = new Date(formYear + '-' + formMonth + '-' + formDay);
birthday = Date.parse(birthday); // NaN
将最后一行更改为:
birthday = Date.parse(birthday.toString());
更新:
OP 评论说问题出在 new Date()
的非标准日期字符串输入上,而不是对 Date.parse()
.
日期字符串输入已修复,但对 Date.parse()
的调用仍然错误(尽管某些浏览器可能仍然可以使用它)。
// this is correct
birthday = new Date(2001, 2, 4);
console.log(typeof birthday); // object
// this is NOT correct, argument to Date.parse is type 'object'
// (Date.parse argument must be type 'string')
birthday = Date.parse(birthday);
// this is what you want:
let birthTime = birthday.getTime();
Date.parse 需要一个字符串参数,来自 MDN:
dateString
A string representing a simplification of the ISO 8601 calendar date extended format. (Other formats may be used, but results are implementation-dependent.)
即使将有效的日期字符串传递给 Date.parse,您也应该避免完全使用 Date.parse,因为不同的主机解析日期字符串的方式不同。来自 MDN:
It is not recommended to use
Date.parse
as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).
您的代码中的问题是当您使用 2002 + '-' + 07 + '-' + 04
时有两个问题,首先,当您定义一个带有前导零 (0) 的数字时,它认为该数字是一个八进制整数,因此您可以喜欢 09
或 08
。
其次,如果你计算 2002 + '-' + 07 + '-' + 04
它将是 2002-7-4
,这不是 JS 规范文档的有效日期,Chrome 和 Firefox 足够聪明,可以将这个字符串解析为日期值,但 Safari 认为这是一个无效值。
你可以这样做:
let birthday = new Date(2002,7,4);
此外,您定义了两次 birthday
,因此只需定义一次。
let birthday = new Date(2002,7,4); // example
birthday = Date.parse(birthday);