You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to check if a person is exaclty or over 18 years old. So I used the diff() and duration() functions to calculate the age of person. I wrote some tests with boundry values to test my logic. I wanted to test if I am exactly one day younger than 18 and exactly 18. So I chose two birthdays for testing (today is 27.02.2017):
exactly 18: 27.02.1999
one day under 18: 28.02.1999
Reproduce
// today date is: 27.02.2017// exactly age 18 varbirthday=moment("27.02.1999","DD.MM.YYYY"),age=moment.duration(moment().diff(birthday)).asYears();console.log(age);// output: 18.00325100985672; expected: == 18// one day under age 18varbirthday=moment("28.02.1999","DD.MM.YYYY"),age=moment.duration(moment().diff(birthday)).asYears();console.log(age);// output: 18.00053005036735; expected: < 18
I think the calculation is correct, because of leap-years. But in terms of birthday, the person shouldn't be 18 years old. Maybe there's a other way to do it with momentjs and I'm doing it wrong. If so, it would be nice to be mentioned in documentation of momentjs.
Production Code
<formid="newsletter-form" data-parsley-validate=""><inputid="birthday" type="text" class="form-control" required="" data-parsley-minage="18"><inputtype="submit" class="btn btn-default" value="subscribe"></form><scripttype="text/javascript">$(function(){window.Parsley.addValidator('minage',{validateString: function(value,minAge){varbirthday=moment(value,"DD.MM.YYYY"),age=moment.duration(moment().diff(birthday)).asYears();return(age>=minAge);},requirementType: 'integer',messages: {en: 'You must be mature.'}});});</script><scripttype="text/javascript">$(function(){$('#newsletter-form').parsley()// field validation.on('form:submit',function(){returnfalse;// do not submit});});</script>
Environment
Chrome Version 56.0.2924.87 (64-bit) on Windows 7
Other information that may be helpful
Timezone of machine: "(UTC+01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien"
The time and date at which the code was run: "Mon Feb 27 2017 13:38:04 GMT+0100"
Other libraries in use: jquery-3.1.1, bootstrap-3.3.7, bootstrap-datepicker-1.6.4, parselyjs-2.6.2
console.log((newDate()).toString())// Mon Feb 27 2017 13:38:04 GMT+0100 (Mitteleuropäische Zeit)console.log((newDate()).toLocaleString())// 27.2.2017, 13:38:04console.log((newDate()).getTimezoneOffset())// -60console.log(navigator.userAgent)// Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36console.log(moment.version)// 2.17.1
The text was updated successfully, but these errors were encountered:
// today date is: 03.03.2017// exactly age 18 varbirthday=moment("03.03.1999","DD.MM.YYYY"),age=moment().diff(birthday,'years');console.log(age);// output: 18; expected: == 18// one day under age 18varbirthday=moment("04.03.1999","DD.MM.YYYY"),age=moment().diff(birthday,'years');console.log(age);// output: 17; expected: < 18
Description of the Issue and Steps to Reproduce
I want to check if a person is exaclty or over 18 years old. So I used the
diff()
andduration()
functions to calculate the age of person. I wrote some tests with boundry values to test my logic. I wanted to test if I am exactly one day younger than 18 and exactly 18. So I chose two birthdays for testing (today is 27.02.2017):Reproduce
I think the calculation is correct, because of leap-years. But in terms of birthday, the person shouldn't be 18 years old. Maybe there's a other way to do it with momentjs and I'm doing it wrong. If so, it would be nice to be mentioned in documentation of momentjs.
Production Code
Environment
Chrome Version 56.0.2924.87 (64-bit) on Windows 7
Other information that may be helpful
The text was updated successfully, but these errors were encountered: