There's got to be an easier, built-in way to instantiate a local date in JavaScript, right? I understand that timezones are a pain, and I see how UTC is a good way to make date instantiation easier from the browser's point of view, but, um... http://jsfiddle.net/rufwork/8Mc8Y/
/*jslint evil:true*/
/*global document*/
function getLocalDate(strDateRepresentation) {
    var dteTemp, milliUtc, milliOffset;

    dteTemp = new Date(strDateRepresentation);
    milliUtc = dteTemp.getTime();
    milliOffset = 1000 * 60 * dteTemp.getTimezoneOffset();    
    // has to be for THIS date or calc could be off -- EST vs. EDT, eg.
    // http://tinyurl.com/d5vzqj9
    // (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/
    //          Global_Objects/Date/getTimezoneOffset)
    // "Daylight saving time prevents this value from being a constant 
    //          even for a given locale"

    return milliUtc + milliOffset;
}

var milliLocal = getLocalDate('2011-01-01');
document.write(milliLocal + "<br>");
document.write(new Date(milliLocal) + "<br>");

document.write("<br>");

milliLocal = getLocalDate('2013-03-01');
document.write(milliLocal + "<br>");
document.write(new Date(milliLocal) + "<br>");

Labels: ,