无法在移动浏览器中显示日期差异
Can't show date difference in mobile browsers
在我的应用程序中,我需要为此计算人员的年龄,我正在获取当前日期和他们的生日(以毫秒为单位)。计算日期差异并将其转换为年份
$scope.getAge=function(birthday){
if (birthday!=undefined&&birthday!="") {
var date = birthday.split('-');
var currentDate = new Date();
var convertedBD= new Date(date[1]+"-"+date[0]+"-"+date[2]);
var diff = Math.abs(currentDate-convertedBD)/(1000*60*60*24*365);
return Math.round(diff);
}
else {
return "0";
}}
这在 Google Chrome 中的桌面浏览器和移动预览中完美运行,但是当我在真实的移动设备上测试它时,它显示每个值都是 NaN。我不知道是什么问题。
尝试 .getTime(),它明确要求日期的毫秒部分:
var diff = Math.abs(currentDate.getTime() - convertedBD.getTime())/(1000*60*60*24*365);
试试这个解析:
if (birthday!=undefined&&birthday!="") {
var date = birthday.split('-');
var currentDate = new Date();
var convertedBD= new Date( parseInt(date[2]), parseInt(date[1]), parseInt(date[0]) );
var diff = Math.abs(currentDate-convertedBD)/(1000*60*60*24*365);
return Math.round(diff);
}
在我的应用程序中,我需要为此计算人员的年龄,我正在获取当前日期和他们的生日(以毫秒为单位)。计算日期差异并将其转换为年份
$scope.getAge=function(birthday){
if (birthday!=undefined&&birthday!="") {
var date = birthday.split('-');
var currentDate = new Date();
var convertedBD= new Date(date[1]+"-"+date[0]+"-"+date[2]);
var diff = Math.abs(currentDate-convertedBD)/(1000*60*60*24*365);
return Math.round(diff);
}
else {
return "0";
}}
这在 Google Chrome 中的桌面浏览器和移动预览中完美运行,但是当我在真实的移动设备上测试它时,它显示每个值都是 NaN。我不知道是什么问题。
尝试 .getTime(),它明确要求日期的毫秒部分:
var diff = Math.abs(currentDate.getTime() - convertedBD.getTime())/(1000*60*60*24*365);
试试这个解析:
if (birthday!=undefined&&birthday!="") {
var date = birthday.split('-');
var currentDate = new Date();
var convertedBD= new Date( parseInt(date[2]), parseInt(date[1]), parseInt(date[0]) );
var diff = Math.abs(currentDate-convertedBD)/(1000*60*60*24*365);
return Math.round(diff);
}