I created some functionality for JavaScript datepicker.
I'm trying to store a value in the datepicker
with the selected month along with which half it is. To do this, I wrote if/else
statements to account for every month.
Among the rules I noted were:
- 28 days - February
- 30 days - April, June, September, November
- 31 days - January, March, May, July, August, October, December
I'm left with some horribly inefficient code:
$('.datepicker').click(function() {
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var date = $('.datepicker').val();
var firstChar = date.match('[a-zA-Z]');
var index = date.indexOf(firstChar);
if ((Number(date.slice(3,5)) < 15) || (index === 0)) {
if (Number(date.slice(0,2)) === 1 || date.indexOf("Jan") === 0) {
$(this).val(months[0] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 2 || date.indexOf("Feb") === 0) {
$(this).val(months[1] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 3 || date.indexOf("Mar") === 0) {
$(this).val(months[2] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 4 || date.indexOf("Apr") === 0) {
$(this).val(months[3] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 5 || date.indexOf("May") === 0) {
$(this).val(months[4] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 6 || date.indexOf("Jun") === 0) {
$(this).val(months[5] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 7 || date.indexOf("Jul") === 0) {
$(this).val(months[6] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 8 || date.indexOf("Aug") === 0) {
$(this).val(months[7] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 9 || date.indexOf("Sep") === 0) {
$(this).val(months[8] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 10 || date.indexOf("Oct") === 0) {
$(this).val(months[9] + "(1-15)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2) === 11) || date.indexOf("Nov") === 0) {
$(this).val(months[10] + "(1-15)" + "," + new Date().getFullYear());
}
else {
$(this).val(months[11] + "(1-15)" + "," + new Date().getFullYear());
}
}
else {
if (Number(date.slice(0,2)) === 2 || date.indexOf("Feb") === 0) {
$(this).val(months[1] + "(16-28)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 4 || date.indexOf("Apr") === 0) {
$(this).val(months[3] + "(16-30)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 6 || date.indexOf("Jun") === 0) {
$(this).val(months[5] + "(16-30)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 9 || date.indexOf("Sep") === 0) {
$(this).val(months[8] + "(16-30)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 11 || date.indexOf("Nov") === 0) {
$(this).val(months[10] + "(16-30)" + "," + new Date().getFullYear());
}
else {
if (Number(date.slice(0,2)) === 1 || date.indexOf("Jan") === 0) {
$(this).val(months[0] + "(16-31)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 3 || date.indexOf("Mar") === 0) {
$(this).val(months[2] + "(16-31)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 5 || date.indexOf("May") === 0) {
$(this).val(months[4] + "(16-31)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 7 || date.indexOf("Jul") === 0) {
$(this).val(months[6] + "(16-31)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 8 || date.indexOf("Aug") === 0) {
$(this).val(months[7] + "(16-31)" + "," + new Date().getFullYear());
}
else if (Number(date.slice(0,2)) === 8 || date.indexOf("Oct") === 0) {
$(this).val(months[9] + "(16-31)" + "," + new Date().getFullYear());
}
else {
$(this).val(months[11] + "(16-31)" + "," + new Date().getFullYear());
}
}
}
});
I'm trying to store a value based on the time range a date falls into. I ended up having to manually enter every condition. Is there a way to make this more efficient?
I thought about using a for
loop, but I think I have too much going on. Not only do I have to account for the variable i
but also of the individual month abbreviations (jan, feb, etc.)