iPad上的Javascript年龄计算 [英] Javascript age calculation on IPad
本文介绍了iPad上的Javascript年龄计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在每个常规浏览器上,日期都是按正常方式计算的,例如38岁.
On every regular browser, the date is calculated normally .. like 38 year old.
在iPad上,我收到一个非数字(NaN)错误...但是为什么?
On IPad I get a Not a Number (NaN) error... but why?
function getAge(date) {
var today = new Date();
var birthDate = new Date(date);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
NaN @ iPad,IE8,并且可以在Firefox& Chrome正常工作
NaN @ IPad, IE8 and correct working at Firefox&Chrome
推荐答案
您需要在输入日期字符串上进行以下转换,以使其在iPad/iPhone上正常工作.
You need to do the following transformations on the input date string for it work properly on iPad/iPhone.
function getAge(date) {
date = date.replace(/-/,"/").replace(/-/,"/"); //substitute - with /
var today = new Date();
var birthDate = new Date(date);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
这篇关于iPad上的Javascript年龄计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文