Reputation: 554
I have a javascript date such as the one below, which is "now" plus say 20 mins:
var end = new Date('04/14/2013 14:00');
Which is being used in a count down script to this time (a delivery ETA).
However this relies upon the clients machine to work out when the countdown should finish, which may not be in GMT or GMT +1 for daylight saving hours.
I looked at the UTC method here http://www.w3schools.com/jsref/jsref_utc.asp , but frankly got lost.
So my question is:
How can I adjust the variable end to account for a possible time difference between GMT and their local settings on the clients machine so that it can at trusted?
Note: "trusted" is a strong word here, maybe "more likely to be accurate" may be better :) Also this needs to be pure JS no jQuery.
Matt
Upvotes: 1
Views: 1346
Reputation: 10148
This perhaps doesn't answer the question directly and maybe I've missed something but it looks like you are over-complicating the problem anyway - if you are using a date as part of a countdown timer then you must always know what the offset (countdown) is at any particular moment.
Since Date
Always returns the local time if you do not pass any parameters to the constructor, why not just determine your "end" date client-side based only on your offset. Eg.
var offset = 1000*60*20; // 20 minutes (in milliseconds)
var end = new Date( new Date().getTime() + offset );
This way you never need to worry about the local timezone or even DST.
Upvotes: 0
Reputation: 665121
I looked at the UTC method, but frankly got lost.
No, using the Date.UTC
function is fine and the way to go. In your case, it would be
var end = new Date(Date.UTC(2013, 3, 14, 14, 0)); // notice months are zero-based
Of course you could use the milliseconds since epoch directly, especially if you generate the date value dynamically:
var end = new Date(1365948000000);
Upvotes: 1