例如,实际年龄应该返回是47 ...使用此代码返回我48.我在做应用逻辑和计算days, months and year.
此代码中的逻辑有什么问题?
0
A
回答
2
假设12个月30天是一年 - 这是最错误的(这是360天,而实际上1年平均接近365.25天)。
什么,你应该做的,分别计算各段:什么输入值
var now = new Date();
var years = now.getFullYear()-formattedDate.getFullYear();
var months = now.getMonth()-formattedDate.getMonth();
var days = now.getDate()-formattedDate.getDate();
if (months < 0) {
months += 12;
years -= 1;
}
if (days < 0) {
months -= 1;
// now days here is a little trickier - we need the number of days in last month
now.setTime(now.getTime() - now.getDate()*24*60*60*1000);
days += now.getDate(); // <-- now is last day of last month now, so we know how many days there were and add this number
}
+0
我没有解释如何添加您的代码与我的...以及总共返回什么.. –
1
的年龄在第一次看一年是正道365,25天,而不是30 * 12 = 360(至少在公历中)
1
google: date diff js
将帮助举例。
注:
- 个月未在30天了!
- 由于DST,日子(在某些应用程序中)还没有24小时。惊喜!
P.S. ExtJS有非常好的日期处理使用情况。
相关问题
- 1. 代码的逻辑有什么问题?
- 2. 我的代码的逻辑有什么问题?
- 3. 我的代码和/或逻辑有什么问题?
- 4. 此javascript代码有什么问题
- 5. 此代码有什么问题
- 6. 此JavaScript代码有什么问题?
- 7. 此Javascript代码有什么问题?
- 8. 我的逻辑有什么问题?
- 9. 我的逻辑有什么问题?
- 10. 我的IF逻辑有什么问题?
- 11. C++我的逻辑有什么问题?
- 12. PHP特质:此代码中的逻辑错误是什么
- 13. 我的do ...逻辑和继续逻辑有什么问题?
- 14. 我的代码在逻辑上出了什么问题?
- 15. 这个mySQL逻辑有什么问题?
- 16. 以下c逻辑有什么问题?
- 17. 这个验证码的逻辑有什么问题?
- 18. 我的加泰罗尼亚号码逻辑有什么问题?
- 19. 这段代码的逻辑是什么?
- 20. 此代码中的Firebase数据库有什么问题?
- 21. 此代码中的if/else语法有什么问题?
- 22. 代码有什么问题
- 23. 代码有什么问题?
- 24. 代码有什么问题
- 25. 代码有什么问题?
- 26. 此代码的Swift 3.1 eri- tion逻辑
- 27. python中的回文逻辑:这个程序有什么问题?
- 28. 在轨道散列中我的逻辑有什么问题?
- 29. <逻辑:迭代问题
- 30. 此代码的排序有什么问题?
实际年龄回报47? – Eineki
我不确定问题是什么,但这里有一个由用户CMS发布的替代方案,他知道他的Javascript:[用JavaScript计算年龄](http://stackoverflow.com/q/4060004) –
什么是12 * 30 ?这当然不是一年中的天数。在一个月内你的天数有一些问题计算至少... – Niklas