0

I'm looking to compare the client's system time (using javascript) with the server's time (in C#). Currently I'm grabbing the client's time using:

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

which returns the times in milliseconds since some date.

Then I'm running DateTime.Now on the server to get it's time. This is the last thing done on the client and the first thing done on the server. The difference will then be added as an element to the browser fingerprint.

I'm not sure if this is the best way to be finding these times, or what the best way is to compare these to times are. I'm looking for a way to compare the difference in milliseconds of the time I got from the client and the time I got from the server, whether that be through converting the different formats of time I get, or requesting the time differently.

  • Is a [network Time Protocol](https://fr.wikipedia.org/wiki/Network_Time_Protocol) implented on your network ? – Graffito Jul 06 '15 at 21:16
  • 1
    You’re probably looking for clock synchronization; in that case, here’s something to read: https://en.wikipedia.org/wiki/Network_Time_Protocol – poke Jul 06 '15 at 21:16

1 Answers1

1

The actual calculation depends on the place you would want to calculate it (on the client or on the server). The most important thing though is to decide in which format you'd want to do the actual diffing. JavaScript returns the number of miliseconds since the UNIX epoch when using getTime(). You could easily calculate the same time object in C#, check out this link for more info on calculating the UNIX epoch in C#. This would give you two variables with a number of milliseconds that can easily be used to add/subtract to compare the difference in time between the server and client.

You also probably should check the difference in timezones between the server and client. You can get the client's local timezone offset to the UTC timezone by using:

var offset = new Date().getTimezoneOffset();

edit: @RobG justly points out that the UNIX epoch is always based on UTC, so there would be no need for time zone calculations.

Community
  • 1
  • 1
Jelle Kralt
  • 980
  • 6
  • 16
  • And how does it solve `Compare Difference in Client and Server System times ` – EZI Jul 06 '15 at 22:37
  • Like I said, whether you do the compare / diff on the client or server, you should first choose an equal date format. I suggest using JavaScript's default way of calculating time and use C# to calculate the same. If you do this you have two numbers that represent time since the Unix epoch, which can be compared by a simple subtraction. – Jelle Kralt Jul 06 '15 at 22:47
  • One number is on server side, the other on client. How would you compare them? – EZI Jul 06 '15 at 22:48
  • The question does not specify anything about it being unclear how the data should be passed from server to client or vice versa. @faithfulpheasant is asking about the best way of finding and comparing these times: **"I'm not sure if this is the best way to be finding these times, or what the best way is to compare these to times are."** – Jelle Kralt Jul 06 '15 at 22:57
  • Both systems use the same epoch in UTC, so time zone should be irrelevant. The ECMAScript time zone is also the opposite sign and in minutes compared to ISO offsets, e.g. UTC+10:00 is -600 (minutes) in ECMAScript. – RobG Jul 06 '15 at 23:00
  • @JelleKralt So you answer an unclear question without knowing what is being asked? – EZI Jul 06 '15 at 23:01
  • @EZI The question isn't unclear to me at all, that's why I gave this answer. – Jelle Kralt Jul 06 '15 at 23:15
  • @RobG You are right of course, I was confused by the time being in the client's time zone. But the milliseconds since the Unix epoch are indeed in UTC. – Jelle Kralt Jul 06 '15 at 23:18
  • @JelleKralt And I still don't see any explanation about *Compare Difference in **Client** and **Server** System times* – EZI Jul 06 '15 at 23:20
  • @EZI I guess we'll have to disagree on this. You focus on the title of the question, but the explanation clearly describes that the question is about C# on the **Server** and JavaScript on the **Client** – Jelle Kralt Jul 06 '15 at 23:29
  • @JelleKralt No, I am reading the question again and again, What I see is OP wants to calculate time difference between his server and client machine. If I am wrong I will undo my downvote. – EZI Jul 06 '15 at 23:32
  • @EZI OP accepted my answer as correct. That being said, I definitely understand where the confusion was coming from. – Jelle Kralt Aug 18 '15 at 04:55