2

I have a javascript application which receives data from ASP.NET WebService in JSON format. My application has a lot of manipulations with dates which it also receives from WebService.

WebService stores all dates in EST timezone and sends them in such format:

{"Date":"\/Date(1319205600000+0300)\/"} 

to my javascript application.

On the client side I should display all dates also in EST timezone irrespectively of browser's timezone. So if I receive from the server the representation of:

10/21/2011 10:00

I should display exactly the same time to the user.

So to convert dates I do something like this:

function convert_date(millisec) {
    var date = new Date(millisec),
        local_offset = date.getTimezoneOffset(),
        server_offset = -5 * 60, //EST offset
        diff = (local_offset + server_offset) * 60000;

    return new Date(millisec + diff);
}

But the point is that server_offset not always should be -5. It can be -4 depending on DST value. I've tried to do server_offset = (date.isDST() ? -4 : -5) * 60 instead but I haven't found any solution for capturing isDST() which works fine for all the local client timezones. Most of them work fine for that local browser's timezone which has the same value of DST as EST timezone but would fail in the case of GMT+5:30 timezone for example.

So are there any way to determine whether DST would be applied for some specific date in EST timezone from javascript?

Or maybe I've missed something?

3
  • Can you change the implementation of the service? Commented Oct 8, 2011 at 15:35
  • FYI those aren't ticks, those are milliseconds Commented Oct 8, 2011 at 15:37
  • @Sii I can ask to do some changes if they are possible. The best solution would be to store dates in UTC I guess, but I'm not sure that it is possible. Commented Oct 8, 2011 at 17:02

2 Answers 2

1

If you can modify the web service, I would have it also return a flag indicating whether or not it was daylight saving time in the server's timezone (EST).

Assuming you can't do this, you can determine whether it is daylight saving time according to this information for DST in the United States:

DST starts on the second Sunday of March and it ends on the first Sunday of November.

The caveat being that this could change in the future (I wasn't even aware it had changed in 2007).

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this info might help. Maybe you can suggest some library that can help me determine dates for second Sunday of March and first Sunday of November? :)
@aliona, unfortunately I can't -- js isn't my primary language so I'm not very familiar with libraries for it. If you go down this path, I think your best bet will be to post a new question, which can be more specific, "How to determine 2nd Sunday in March in JavaScript".
thanks! your suggestion worked for me. For dates manipulation purpose I've picked datejs library. Seems like now everything works fine. Thanks again.
@aliona you're welcome, and thanks for letting me know about datajs :)
0

I am not really sure how exactly you are getting the ticks value you pass to the function, which I think you have something weird going on there. But if the question is how to write the isDST() function, then that should be pretty simple.

Date.prototype.isDST = function()
{
    var dt = new Date(this.getYear(), 1, 1);
    return dt.getTimezoneOffset() > this.getTimezoneOffset();
}

edit: This isn't polished/tested for negative offsets.

3 Comments

I believe your code would give me DST value for user's browser timezone. It's not what I'm looking for. I need to know DST value for EST timezone.
You want to know if server time is on DST? Why? If you are able to parse the value given to you, then you already get correct local time with correct offset.
Server stores all dates in EST. And when I recieve some certain date I need to determine if this date is on DST for EST timezone, but not for local one. If I know the DST value true/false I will know correct offset for EST: -4 or -5. All this information I should know to be able to convert dates correctly and get exactly the same datetime as it would be for EST even if clientside timezone is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.