-1

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();
    }
}

http://jsfiddle.net/q98sLy5c/1/

triplethreat77
  • 1,276
  • 8
  • 34
  • 69
  • RE: Bonus, moment is a popular lib for easy date formatting http://momentjs.com/ – BReal14 Sep 05 '14 at 14:17
  • possible duplicate of [Alternative to a million IF statements](http://stackoverflow.com/q/10029089/1048572) – Bergi Sep 05 '14 at 14:24

3 Answers3

0

Use a switch statement:

The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.

var result = false;

swtich ( $('#state').val() ) {
    /* If the value is "CO"... */
    case 'CO':
        result = pacifictime + " " + ampm;
        break;
    /* If the value is "TX" or "LA"... */
    case 'TX':
    case 'LA':
        result = entraltime + " " + ampm;
        break;
    default:
        break;
}

/* If the result was set, display the result. */
if (result)
    $('#currenttime').show().val( result );
/* Otherwise, hide it. */
else
    $('#currenttime').hide();
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

If you are trying to go from state to timezone using an object would probably be your most time efficient means.

var stateToTimeZone = {};

stateToTimeZone['TX'] = entraltime + " " + ampm;
stateToTimeZone['LA'] = entraltime + " " + ampm;
stateToTimeZone['CO'] = pacifictime + " " + ampm;
 . . .
var result = stateToTimeZone[$('#state').val()];
if (result)
    $('#currenttime').show().val( result );
/* Otherwise, hide it. */
else
    $('#currenttime').hide();

Basically, you are trading a faster time for more memory usage. But I think this is what you would want to do in this case. It could be easily expanded for counties also, just use a key like "WA_King", since the same county name could appear in different states.

catalyst294
  • 129
  • 9
0

You can use the indexOf() method on your array like this:

if (pacificstates.indexOf($('#state').val()) !== -1)

If that call returns -1 that means the element is not contained in that array.

See here for working example http://jsfiddle.net/q98sLy5c/2/

user145400
  • 1,076
  • 1
  • 12
  • 27