Question

I have an application that is used from various time zones. If someone is entered into the database in New York (Eastern Time) it should display the person entered in New York time (which it does through most of the application). However, when someone from outside of timezone views it, it seems the time is converted to that user's time zone.

I am trying to either stop the conversion or make the conversion to the correct time. It is passed to the javascript correctly, it is in the javascript where the issue is found.

The date string for the object shows (for example) "Fri Aug 21 12:30:00 PDT 2013".

I have modified this function to work with US Daylight Savings Time and to use the central time zone. I have also used this function in order to process the date from the format to the unix epoch.

I was thinking that if the "PDT" or any other time zone identifier ("CST", "MST", "PST", etc..) then maybe the conversion wouldn't happen. I will note that the time stamp other than that "PDT" is the correct time I need, but something happens where it displays it 2 hours earlier.

My main issue is if someone is entered at 1pm on 7/24/2013 in New York, it should still say that regardless of which timezone I am in when I view that data. Here is the problem:

  1. User is entered in the system from a location (it's not always New York, but it's easy to use as an example) on 7/24/2013 at 3:00pm.
  2. It is sent from the database and through the application to the javascript correctly (7/24/2013 3:00pm)
  3. If I am in adifferent time zone (let's say central) the date is shown in theobject in the javascript as (Wed Jul 24 3:00 CDT 2013). <--As you see still techically correct other than that timezone identifier of "CDT". If the time is generally the same, if there was someway to delete that identifier out of it, I think I would be golden.
  4. No other function is called and it now shows the date as 7/24/2013 2:00.

Any thoughts would be extremely helpful. Here is my conversion function:

function calcTime(date) {
//This is to pad the date with a zero if the
//number is less than 10. eg 7/1/2013 = 07/01/2013
function pad(num) {
    num = num.toString();
    if (num.length == 1) return "0" + num;
    return num;
}

//I needed to have my date passed in as string
//This will set the date to the string and then get the time from
//that date. It will then set that time.
var date = new Date(date);
var time = date.getTime();
date.setTime(time);

var year = date.getUTCFullYear();

//This nifty little thing will get you the daylight savings time
// and adjust correctly. In the US, prior to 2006 the DST started
//first sunday in April and ended in the last sunday in October.
//After 2007, it was changed to start on the 2nd sunday in March
//and end in the first sunday in November. If you are working
//with a different country, please follow this link for
//the equations in relation to their daylight savings time.
//http://www.webexhibits.org/daylightsaving/i.html
if (year <= 2006) {
    var start_day = (2 + ((((6 * year) - year / 4) % 7) + 1));
    var DST_start = new Date(Date.UTC(year, 3, start_day, 1, 0, 0));

    var end_day = (31 - ((((5 * year) / 4) + 1) % 7));
    var DST_end = new Date(Date.UTC(year, 9, end_day, 1, 0, 0));
}

if (year >= 2007) {
    start_day = (14 - ((1 + (year * 5) / 4) % 7));
    DST_start = new Date(Date.UTC(year, 2, start_day, 1, 0, 0));

    end_day = (7 - ((1 + (year * 5) / 4) % 7));
    DST_end = new Date(Date.UTC(year, 10, end_day, 1, 0, 0));
}

//This function is supposed to make sure no matter where you are,
//you can see this in US central time. Obviously you will need to
//change this if you need a different time zone.
//All timezones to the west of GMT to the International Date Line will be negative.
//Timezones east of GMT to the International Date Line will be positive.
//The below offset is if it is DST, it adjusts the time accordingly.
var centralOffset = -6 * 60 * 60 * 1000;
if (date > DST_start && date < DST_end) centralOffset = -5 * 60 * 60 * 1000;

date.setTime(time + centralOffset);

//This will call the pad function to return a two digit month/day format.
//Dates are zero based in JS and needed to add 1 to the UTCMonth so we receive the
//current month
var centralTime = pad(date.getUTCMonth() + 1) + "/" +
pad(date.getUTCDate()) + "/" + date.getUTCFullYear() +
" " + pad(date.getUTCHours()) + ":" +
pad(date.getUTCMinutes()) + ":" + pad(date.getUTCSeconds());

return centralTime;
};

//Test Data
var date = "7/25/2013 09:30:00";
calcTime(date);

UPDATE: I have not had much luck. I have been able to successfully remove the time zone identifier, but when I have this new string of correct date and try to pass it in to a new Date function, it puts the identifier back in. Please see the updated code below.

function getDateTime() {
  function pad(num) {
    num = num.toString();
    if (num.length == 1) return "0" + num;
    return num;
}


var timezones = ["PST", "PDT", "MST", "MDT", "CST", "CDT", "EST", "EDT", "GMT"];

var date = this; //this = "Wed Aug 21 14:38:00 CST 2013" or whatever date is being passed in

//This converts date to string so it can remove the time zone identifier.
date = date.toDateString();

var length = timezones.length;
while (length--) {
    if (date.indexOf(timezones[length]) != -1) {
        date = date.replace(timezones[length], "");
    }
}

date = new Date(date); // This pretty much defeats all that I did above.
//date = Date(date); //This will give the current date and time.

var month = pad(date.getMonth() + 1);
var day = pad(date.getDate());
var year = date.getFullYear();
var hour = pad(date.getHours());
var minute = pad(date.getMinutes());
var second = pad(date.getSeconds());


if (hour > 12) {
    var timestring = hour.toString() + ":" + minute.toString() + ":" + second.toString() + " PM";
} else {
    timestring = hour.toString() + ":" + minute.toString() + ":" + second.toString() + " AM";
}

var output = month.toString() + "/" + day.toString() + "/" + year.toString() + " " +   timestring;

return output;

}

Hopefully this clears up any confusion anyone else may have had from the original post.

Was it helpful?

Solution

Use multiple string substitutions to preserve the original string:

var foo = "2013-10-01T09:00:00.000-04:00";
var bar = foo.substr(-6);
var baz = foo.substr(11,2)
var isoDate = new Date(foo).toISOString().replace("Z",bar).replace(/T../,"T"+baz);
var hours = String(baz).concat(Number(baz) < 12 ? "PM" : "AM").replace(/^0/,"")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top