1

I'm writing javascript multiplayer game, my server code is in nodejs. I need to synchronize time between server and client.

At server side i call Date() and send the date string to client. Client recieves:

Tue Apr 28 2015 15:37:01 GMT+0000 (UTC)

After initialization with:

var time = Date("Tue Apr 28 2015 15:37:01 GMT+0000 (UTC)");

the time variable set to local time is:

Tue Apr 28 2015 17:37:03 GMT+0200 (CEST).

If i run my server localy then time is importing correctly. Is problem in different time format, CEST vs UTC ?

Thank you for answers

1
  • time has no format. Only when you show (stringify) it, it will use the local timezone for formatting the string. If you explicitly use time.toUTCString(), it won't. Commented Oct 22, 2015 at 7:31

2 Answers 2

1

There are various ways to handle the timezones in JS.

Set UTC time across system

use .setUTCHours() Here you can see how to use it.

Create a new UTC Date

new Date(Date.UTC(year, month, day, hour, minute, second))

Date.UTC is explained here

Correct the difference in timezones manually

var d = new Date(xiYear, xiMonth, xiDate);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
Sign up to request clarification or add additional context in comments.

Comments

0

You can send from server date in milliseconds format and then converting milliseconds to the date in required format.

Server:

var time = new Date().getTime();

Cleint:

var date = new Date(time);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.