์ฌ๋์ด ์ ํํ์ง ๋๋ 18์ธ ์ด์์ธ์ง ํ์ธํ๊ณ ์ถ์ต๋๋ค. ๊ทธ๋์ diff()
์ duration()
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋์ ๋์ด๋ฅผ ๊ณ์ฐํ์ต๋๋ค. ๋ด ๋
ผ๋ฆฌ๋ฅผ ํ
์คํธํ๊ธฐ ์ํด ๊ฒฝ๊ณ ๊ฐ์ผ๋ก ๋ช ๊ฐ์ง ํ
์คํธ๋ฅผ ์์ฑํ์ต๋๋ค. ๋ด๊ฐ 18์ธ๋ณด๋ค ์ ํํ ํ๋ฃจ ์ด๋ฆฌ๊ณ ์ ํํ 18์ธ์ธ์ง ํ
์คํธํ๊ณ ์ถ์์ต๋๋ค. ๊ทธ๋์ ํ
์คํธ๋ฅผ ์ํด ๋ ๊ฐ์ ์์ผ์ ์ ํํ์ต๋๋ค(์ค๋์ 27.02.2017).
// today date is: 27.02.2017
// exactly age 18
var birthday = 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 18
var birthday = moment("28.02.1999", "DD.MM.YYYY"),
age = moment.duration(moment().diff(birthday)).asYears();
console.log(age); // output: 18.00053005036735; expected: < 18
์ค๋ ๋๋ฌธ์ ๊ณ์ฐ์ด ๋ง๋ค๊ณ ์๊ฐํฉ๋๋ค. ๊ทธ๋ฌ๋ ์์ผ์ ๊ดํด์๋ ๊ทธ ์ฌ๋์ด 18 ์ธ๊ฐ๋์ด์๋ ์๋ฉ๋๋ค. ์๋ง๋ momentjs๋ก ๊ทธ๊ฒ์ ์ํํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์๊ณ ๋ด๊ฐ ์๋ชปํ๊ณ ์์ต๋๋ค. ๊ทธ๋ ๋ค๋ฉด momentjs์ ๋ฌธ์์ ์ธ๊ธ๋๋ ๊ฒ์ด ์ข์ ๊ฒ์ ๋๋ค.
<form id="newsletter-form" data-parsley-validate="">
<input id="birthday" type="text" class="form-control" required="" data-parsley-minage="18">
<input type="submit" class="btn btn-default" value="subscribe">
</form>
<script type="text/javascript">
$(function () {
window.Parsley.addValidator('minage', {
validateString: function(value, minAge) {
var birthday = moment(value, "DD.MM.YYYY"),
age = moment.duration(moment().diff(birthday)).asYears();
return (age >= minAge);
},
requirementType: 'integer',
messages: {
en: 'You must be mature.'
}
});
});
</script>
<script type="text/javascript">
$(function () {
$('#newsletter-form').parsley()
// field validation
.on('form:submit', function() {
return false; // do not submit
});
});
</script>
Windows 7์ Chrome ๋ฒ์ 56.0.2924.87(64๋นํธ)
console.log( (new Date()).toString()) // Mon Feb 27 2017 13:38:04 GMT+0100 (Mitteleuropรคische Zeit)
console.log((new Date()).toLocaleString()) // 27.2.2017, 13:38:04
console.log( (new Date()).getTimezoneOffset()) // -60
console.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.36
console.log(moment.version) // 2.17.1
๊ณ์ฐ์ด ์๋ชป๋ ๊ฒ ๊ฐ์ง๋ง ๋งค์ฐ ๊ฐ๊น์ต๋๋ค. ์๋ง๋ ๋ฐ์ฌ๋ฆผ ์ค๋ฅ๋ก ์ธํด?
์๋ฅผ ๋ค์ด:
a = moment("27.02.1999", "DD.MM.YYYY");
b = moment("28.02.1999", "DD.MM.YYYY");
c = moment("27.02.2017", "DD.MM.YYYY");
moment.duration(c.diff(a)).asYears(); // 18.00173857094944
moment.duration(c.diff(b)).asYears(); // 17.99900066394245
๋ฐ๋ผ์ ์ด ๊ฒฝ์ฐ moment.duration(end.diff(start)).asYears()
๋ ์ฝ 16์๊ฐ ์ ์ 18
๋ฐํํฉ๋๋ค.
์ฐ๋ น์ด 18์ธ ์ด์์ธ์ง ํ์ธํ๋ ค๋ ๊ฒฝ์ฐ ์ฐ๋, (18์ธ ์ฐจ์ด์ธ ๊ฒฝ์ฐ) ์, ์ผ์ ๋น๊ตํ ์ ์์ต๋๋ค.
// pseudocode, correctness not guaranteed
function is18(start, end) {
if (end.year() - start.year() == 18) {
if (end.month() == start.month()) {
return end.date() >= start.date();
}
return end.month() > start.month();
}
return end.year() - 18 > start.year();
}
๊ทธ๋ฅ ์ฌ์ฉํ๋ฉด ๋ ์ ์๋ํฉ๋๊น?
age = moment().diff(birthday, 'years');
@mj1856
์, ์ ์๋ํฉ๋๋ค.
// today date is: 03.03.2017
// exactly age 18
var birthday = moment("03.03.1999", "DD.MM.YYYY"),
age = moment().diff(birthday, 'years');
console.log(age); // output: 18; expected: == 18
// one day under age 18
var birthday = moment("04.03.1999", "DD.MM.YYYY"),
age = moment().diff(birthday, 'years');
console.log(age); // output: 17; expected: < 18
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๊ทธ๋ฅ ์ฌ์ฉํ๋ฉด ๋ ์ ์๋ํฉ๋๊น?