【问题标题】:how to calculate the age of person using moment js?如何使用moment js计算人的年龄?
【发布时间】:2019-04-17 03:33:03
【问题描述】:
你能告诉我如何使用moment js计算人的年龄吗?
我有两个日期“出生日期”和“当前日期”。我想知道它们之间的差异来获得年龄。
年龄 15 岁,14 岁 ..
这是我的代码
http://plnkr.co/edit/1wIvVISmgEqcRNnAD971?p=preview
let DOB= "09-Feb-1983"
console.log(moment(DOB, 'DD-MMM-YYYY', true).isValid())
console.log(moment().format('DD-MMM-YYYY'));
【问题讨论】:
标签:
javascript
angularjs
momentjs
【解决方案1】:
试试这个
moment().diff('09-Feb-1983', 'years');
【解决方案2】:
找到这个网站https://www.sitepoint.com/managing-dates-times-using-moment-js/
在 Google 搜索的第一个结果中
var dateB = moment('2010-11-11');
var dateC = moment('2014-10-11');
console.log('Difference is ', dateB.diff(dateC), 'milliseconds');
console.log('Difference is ', dateB.diff(dateC, 'days'), 'days');
console.log('Difference is ', dateB.diff(dateC, 'months'), 'months');
console.log('Difference is ', dateB.diff(dateC, 'years'), 'years');