【发布时间】:2013-12-14 19:46:28
【问题描述】:
我设置了一个 jQuery 日期选择器,它接受出生日期,并希望使用选择的日期来输出特定类别,将输入的日期与每个类别的最小和最大日期进行比较。目前我在比较日期时遇到问题,希望能得到一些建议。我现在知道我的问题是因为我正在尝试比较一个字符串和一个字符串,但我想知道我应该将每个日期转换为类似 unix 值的东西来进行比较还是有一些Date()我应该在我的每个 ageLimits
JS
var ageLimits = {
'under10': {
'minAge': '06-05-2005',
'maxAge': '07-04-2004'
},
'under13': {
'minAge': '08-04-2003',
'maxAge': '15-08-2001'
},
'under15': {
'minAge': '16-08-2000',
'maxAge': '07-12-1999'
}
}
function getUserDate(val) {
dateInput = moment( $('#datepicker').val(), 'MM-DD-YYYY');
//pass full date to selectAgeCategory
selectAgeCategory( dateInput.format('MM-DD-YYYY') );
}
function selectAgeCategory(date) {
console.log(date);
if( date >= ageLimits.under10.minAge && date <= ageLimits.under10.maxAge ) {
console.log('Under 10 category');
} else if ( date >= ageLimits.under13.minAge && date <= ageLimits.under13.maxAge ) {
console.log('Under 13 category');
} else if ( date >= ageLimits.under15.minAge && date <= ageLimits.under15.maxAge ) {
console.log('Under 15 category');
} else {
console.log('No category available for this age');
}
}
JSFiddle:http://jsfiddle.net/qA9NT/5/
【问题讨论】:
-
停止以字符串形式存储和比较日期
-
嘿@ryan 是的,我意识到这就是问题所在,但我现在应该怎么做才能解决这个问题?
标签: javascript jquery date