0

Yes, I know about thousands of look-a-like questions, but my brain is trying to blow and I ask you for help. I've read nuomerous articles and still confused.

Here is what I have.

var next_market_event_in_NewYork = "2014-12-02T16:00:00"; //that's what server gives me
var secondsFromMarketToMoscow = 28800; //that's also what server gives me
var secondsFromMoscowToLondon = 10800; //that's also what server gives me 
var secondsFromLocalToLondon = 7200; //that's what I get from local timeoffset new Date().getTimezoneOffset()*(-60)

What I need is how many seconds left from now locally to next event in New York. Spent on this dozens of hours. Just don't get how to get the answer. Pls help.

Prosto Trader
  • 3,471
  • 3
  • 31
  • 52
  • Brian's about to blow? Better call HR... – Marc B Dec 02 '14 at 15:59
  • 1. Take the script from here: http://stackoverflow.com/questions/7971813/get-helsinki-local-time-regardless-of-local-time-zone 2. add the milliseconds to market event to the date – mplungjan Dec 02 '14 at 16:13

2 Answers2

1

The difficult part is calculating the difference between New York time and UTC. If you can do this server-side you should have no problem. If you can't then you can hardcode the offset, but that changes when USA is on Daylight time.

Here's a code example with a hardcoded 5 hour offset between NY and UTC.

var next_market_event_in_NewYork = "2014-12-02T16:00:00";
var msNYtoUTC = 5 * 60 * 60 * 1000; //better to calculate this server-side
var nextEventDate = new Date(Date.parse(next_market_event_in_NewYork) + msNYtoUTC);

setInterval(function() {
  var localTime = new Date();
  var secondsRemaining = (nextEventDate - localTime) / 1000;
  document.getElementById("result").innerHTML = secondsRemaining.toFixed(0);
}, 1000);
<h1>
  <span id="result"></span>
  seconds until market event
</h1>
Dave
  • 10,748
  • 3
  • 43
  • 54
  • next_market_event_in_NewYork - it's not UTC time. It's New Yorks's local time (( – Prosto Trader Dec 02 '14 at 18:06
  • your snippet shows for me that there is -14800 seconds left till 16:00 in New York. But it's only 13:00 now there.... – Prosto Trader Dec 02 '14 at 18:08
  • This won't accommodate daylight saving time. NY is only 5 hours behind UTC during the winter. In the summer, it's 4 hours behind. – Matt Johnson-Pint Dec 02 '14 at 18:53
  • @MattJohnson yes I noted that in the answer and said it is better to calculate that value server-side, which the OP seems to have the means to do. – Dave Dec 02 '14 at 18:54
  • I can't calculate offset server side...I do know offset of each market from moscow and offset of Moscow from London as shown in question. Pls help me understand how do I get msNYtoUTC knowing that – Prosto Trader Dec 02 '14 at 20:15
  • You can use a [javascript function that determines if daylight saving time is in effect](http://stackoverflow.com/a/11888430/361762) and set the offset based on that--if not in effect then the offset is calculated as shown, otherwise change the 5 to a 4. – Dave Dec 02 '14 at 20:49
  • That test only works against the local time zone. It won't tell you accurately whether NY is in DST if you are not in NY. – Matt Johnson-Pint Dec 02 '14 at 21:16
0

If you require a pure JavaScript solution, consider using a library that already understands time zones, such as moment.js with the moment-timezone add on.

var next_market_event_in_NewYork = "2014-12-02T16:00:00";
var m = moment.tz(next_market_event_in_NewYork, "America/New_York");
var now = moment();
var s = m.diff(now,'seconds');
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I know about moment.tz, but actually I don't know timezone of particular market. In my question, I myself assumed that it's New York, but what server gives me is not a timezone - it's market offset from Moscow (not even London). – Prosto Trader Dec 02 '14 at 20:12
  • 1
    I'm confused. You first said the server gave you offsets to Moscow *and* London. Now you said it's Moscow only? Is the server taking into account Moscow's recent shift from UTC+4 to UTC+3? And when you say London, are you referring just to GMT? Or are you taking into account that London is GMT (UTC+0) in the winter and BST (UTC+1) in the summer? Do I understand correctly that you really don't know what time zone the market is in and there are multiple markets? I'm not sure how you expect us to guess. Can you be more specific please? – Matt Johnson-Pint Dec 02 '14 at 21:14