-
Notifications
You must be signed in to change notification settings - Fork 1
/
dob.js
50 lines (44 loc) · 1.37 KB
/
dob.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31],
today = new Date(),
// default targetDate is christmas
targetDate = new Date(today.getFullYear(), 11, 25);
setDate(targetDate);
setYears(20) // set the next five years in dropdown
$("#select-month").change(function() {
var monthIndex = $("#select-month").val();
setDays(monthIndex);
});
function setDate(date) {
setDays(date.getMonth());
$("#select-day").val(date.getDate());
$("#select-month").val(date.getMonth());
$("#select-year").val(date.getFullYear());
}
// make sure the number of days correspond with the selected month
function setDays(monthIndex) {
var optionCount = $('#select-day option').length,
daysCount = daysInMonth[monthIndex];
if (optionCount < daysCount) {
for (var i = optionCount; i < daysCount; i++) {
$('#select-day')
.append($("<option></option>")
.attr("value", i + 1)
.text(i + 1));
}
}
else {
for (var i = daysCount; i < optionCount; i++) {
var optionItem = '#select-day option[value=' + (i+1) + ']';
$(optionItem).remove();
}
}
}
function setYears(val) {
var year = today.getFullYear();
for (var i = 0; i < val; i++) {
$('#select-year')
.append($("<option></option>")
.attr("value", year + i)
.text(year + i));
}
}