I'm trying to calculate a person's age using Moment.js, but I'm finding that the otherwise useful fromNow method rounds up the years. For instance, if today is 12/27/2012 and the person's birth date is 02/26/1978, moment("02/26/1978", "MM/DD/YYYY").fromNow()
returns '35 years ago'. How can I make Moment.js ignore the number of months, and simply return the number of years (i.e. 34) since the date?
我正在尝试使用Moment.js来计算一个人的年龄,但我发现其他有用的fromNow方法可以完成这些年。例如,如果今天是12/27/2012并且该人的出生日期是02/26/1978,那一刻(“02/26/1978”,“MM / DD / YYYY”)。fromNow()返回'35年前”。如何让Moment.js忽略月数,并简单地返回自该日期起的年数(即34)?
9 个解决方案
#1
138
Using moment.js is as easy as:
使用moment.js就像:
var years = moment().diff('1981-01-01', 'years');
var days = moment().diff('1981-01-01', 'days');
For additional reference, you can read moment.js official documentation.
有关其他参考,请阅读moment.js官方文档。
#2
19
http://jsfiddle.net/xR8t5/27/
if you do not want fraction values:
如果你不想要分数值:
var years = moment().diff('1981-01-01', 'years',false);
alert( years);
if you want fraction values:
如果你想要分数值:
var years = moment().diff('1981-01-01', 'years',true);
alert( years);
Units can be [seconds, minutes, hours, days, weeks, months, years]
单位可以是[秒,分钟,小时,天,周,月,年]
#3
17
There appears to be a difference function that accepts time intervals to use as well as an option to not round the result. So, something like
似乎有一个差异函数接受使用的时间间隔以及不对结果进行舍入的选项。所以,像
Math.floor(moment(new Date()).diff(moment("02/26/1978","MM/DD/YYYY"),'years',true)))
I haven't tried this, and I'm not completely familiar with moment, but it seems like this should get what you want (without having to reset the month).
我没有试过这个,我对时刻并不完全熟悉,但看起来这应该得到你想要的东西(无需重置月份)。
#4
12
I found that it would work to reset the month to January for both dates (the provided date and the present):
我发现将两个日期(提供日期和现在)的月份重置为1月:
> moment("02/26/1978", "MM/DD/YYYY").month(0).from(moment().month(0))
"34 years ago"
#5
9
This method is easy and powerful.
这种方法简单而有力。
Value is a date and "DD-MM-YYYY" is the mask of the date.
值是日期,“DD-MM-YYYY”是日期的掩码。
moment().diff(moment(value, "DD-MM-YYYY"), 'years');
#6
5
Try this:
尝试这个:
moment("02/26/1978", "MM/DD/YYYY").fromNow().split(" ")[0];
Explanation:
说明:
We receive string something like this: '23 days ago'. Split it to array: ['23', 'days', 'ago'] and then take first item '23'.
我们收到类似这样的字符串:'23天前'。将其拆分为数组:['23','days','before']然后取第一个项目'23'。
#7
2
This method works for me. It's checking if the person has had their birthday this year and subtracts one year otherwise.
这种方法适合我。它正在检查这个人今年是否过生日,否则减去一年。
// date is the moment you're calculating the age of
var now = moment().unix();
var then = date.unix();
var diff = (now - then) / (60 * 60 * 24 * 365);
var years = Math.floor(diff);
Edit: First version didn't quite work perfectly. The updated one should
编辑:第一个版本并不完美。更新的应该
#8
2
If you dont want to use any module for age calculation
如果您不想使用任何模块进行年龄计算
var age = Math.floor((new Date() - new Date(date_of_birth)) / 1000 / 60 / 60 / 24 / 365.25)
#9
1
When you want to show years and the remaining days:
当您想要显示年份和剩余天数时:
var m = moment(d.birthday.date, "DD.MM.YYYY");
var years = moment().diff(m, 'years', false);
var days = moment().diff(m.add(years, 'years'), 'days', false);
alert(years + ' years, ' + days + ' days');
#1
138
Using moment.js is as easy as:
使用moment.js就像:
var years = moment().diff('1981-01-01', 'years');
var days = moment().diff('1981-01-01', 'days');
For additional reference, you can read moment.js official documentation.
有关其他参考,请阅读moment.js官方文档。
#2
19
http://jsfiddle.net/xR8t5/27/
if you do not want fraction values:
如果你不想要分数值:
var years = moment().diff('1981-01-01', 'years',false);
alert( years);
if you want fraction values:
如果你想要分数值:
var years = moment().diff('1981-01-01', 'years',true);
alert( years);
Units can be [seconds, minutes, hours, days, weeks, months, years]
单位可以是[秒,分钟,小时,天,周,月,年]
#3
17
There appears to be a difference function that accepts time intervals to use as well as an option to not round the result. So, something like
似乎有一个差异函数接受使用的时间间隔以及不对结果进行舍入的选项。所以,像
Math.floor(moment(new Date()).diff(moment("02/26/1978","MM/DD/YYYY"),'years',true)))
I haven't tried this, and I'm not completely familiar with moment, but it seems like this should get what you want (without having to reset the month).
我没有试过这个,我对时刻并不完全熟悉,但看起来这应该得到你想要的东西(无需重置月份)。
#4
12
I found that it would work to reset the month to January for both dates (the provided date and the present):
我发现将两个日期(提供日期和现在)的月份重置为1月:
> moment("02/26/1978", "MM/DD/YYYY").month(0).from(moment().month(0))
"34 years ago"
#5
9
This method is easy and powerful.
这种方法简单而有力。
Value is a date and "DD-MM-YYYY" is the mask of the date.
值是日期,“DD-MM-YYYY”是日期的掩码。
moment().diff(moment(value, "DD-MM-YYYY"), 'years');
#6
5
Try this:
尝试这个:
moment("02/26/1978", "MM/DD/YYYY").fromNow().split(" ")[0];
Explanation:
说明:
We receive string something like this: '23 days ago'. Split it to array: ['23', 'days', 'ago'] and then take first item '23'.
我们收到类似这样的字符串:'23天前'。将其拆分为数组:['23','days','before']然后取第一个项目'23'。
#7
2
This method works for me. It's checking if the person has had their birthday this year and subtracts one year otherwise.
这种方法适合我。它正在检查这个人今年是否过生日,否则减去一年。
// date is the moment you're calculating the age of
var now = moment().unix();
var then = date.unix();
var diff = (now - then) / (60 * 60 * 24 * 365);
var years = Math.floor(diff);
Edit: First version didn't quite work perfectly. The updated one should
编辑:第一个版本并不完美。更新的应该
#8
2
If you dont want to use any module for age calculation
如果您不想使用任何模块进行年龄计算
var age = Math.floor((new Date() - new Date(date_of_birth)) / 1000 / 60 / 60 / 24 / 365.25)
#9
1
When you want to show years and the remaining days:
当您想要显示年份和剩余天数时:
var m = moment(d.birthday.date, "DD.MM.YYYY");
var years = moment().diff(m, 'years', false);
var days = moment().diff(m.add(years, 'years'), 'days', false);
alert(years + ' years, ' + days + ' days');