So I am writing a time zone plug in of sorts and I need to simplify my if statements because I want to avoid repetition. I am currently writing it to change time by states (Yes, I know some states can have more than one time zone. I'll refine this later, possibly by county.)
Instead of saying if value equals this OR this OR this OR this etc can we store all possible values in a variable? Below, I have an array for pacific states, how can I tell if the state value equals one of these?
jQuery
function GetClientTime() {
// current time
var dt = new Date();
// pacific time
var pacifictime = dt.getHours() - 2 + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
// mountain time
var mountaintime = dt.getHours() - 1 + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
// central time
var centraltime = dt.getHours() + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
// eastern time
var easterntime = dt.getHours() + 1 + ":" + (dt.getMinutes() < 10 ? '0' : '') + dt.getMinutes();
// get am/pm
var hours = new Date().getHours();
var ampm = (hours >= 12) ? "PM" : "AM";
var pacificstates = [
"WA", "OR", "CA", "NV"
]
if ($('#state').val() == pacificstates) {
$('#currenttime').show().val(pacifictime + " " + ampm);
} else if ($('#state').val() == 'CO') {
$('#currenttime').show().val(mountaintime + " " + ampm);
} else if ($('#state').val() == 'TX' || $('#state').val() == 'LA') {
$('#currenttime').show().val(centraltime + " " + ampm);
} else if ($('#state').val() == 'NY' || $('#state').val() == 'NJ') {
$('#currenttime').show().val(easterntime + " " + ampm);
} else {
$('#currenttime').hide();
}
}