asdf_enel_hak
asdf_enel_hak

Reputation: 7650

Why is there time difference between client and server on localhost

Application is running on localhost. Server is one hour earlier than client!

Client sends time : Sat Apr 25 2015 00:00:00 GMT-0400 (Eastern Daylight Time)

enter image description here

Request is sent: dateOfArrival: "2015-04-25T04:00:00.000Z"

enter image description here

Server receives time: {4/24/2015 11:00:00 PM}

enter image description here

Why is there one hour difference between and how can I handle it? I could guess it is somehow related Daylight time vs Standart time.

When I try this code:

string dateStr = "2015-04-25T04:00:00.000Z";  
var myDate = DateTime.Parse(dateStr);  // it gives me myDate = {4/25/2015 12:00:00 AM}

Actually I am only interested day part of time. In my db, I am holding it as Date type. But because of this time difference, my days goes one day before.

I tried various ways to handle issue but I got completely lost in datetime conversion world! Even I am lost on localhost application, I could not imagine what will happen on live server.

I think this Q&A also mentions similar issue, but I can't figure it out if it matters:

My timezone: Eastern Time Zone (UTC-05:00)

About web api odata json serializer, from this post I could say that it is other then this one

Here is my server code:

// PATCH: odata/IncomingStudents(5)
[AcceptVerbs("PATCH", "MERGE")]
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<IncomingStudent> patch)
{
    Validate(patch.GetEntity());
    var dateOfArrival = patch.GetEntity().DateOfArrival
... 
}

Client is angularjs sending http patch request

Upvotes: 3

Views: 3819

Answers (3)

asdf_enel_hak
asdf_enel_hak

Reputation: 7650

Mystery is solved:

Value of dateOfArrival in db is: 2015-04-25 (date only)
server takes that value makes it: 2015-04-24T23:00:00-05:00

at the client, new Date(dateOfArrival) gives 2015-04-25T04:00:00.000Z
send this value to server and server makes it 4/24/2015 11:00:00 PM

Server receives the same value.

As stated in this post, if the date is not in the range of day light saving time such as 01/01/2015 all the dates would be the same.

On the other hand, as I work with web api odata, as it works only DateTimeOffset but not DateTime I guess this timezone issues happens

Upvotes: 1

Pablo Romeo
Pablo Romeo

Reputation: 11396

To avoid timezone differences between client and server (or differing configurations within the same machine) you probably want to use UTC for any transfers and for storing it in databases.

Meaning, on the server side, you might need DateTime.ToUniversalTime() to adjust a date, or DateTime.UtcNow to get the current date & time.

From javascript it might be a good idea to use moment.js to ease the handling of dates.

using moment.js, you could get the value in UTC in the following manner:

var localDate = new Date();
var inUtc = moment(localDate).utc();

You'd display information to the user in his local offset, but always send to the server and receive back using UTC.

Upvotes: 2

phil soady
phil soady

Reputation: 11348

The Server code runs as in the user context of user in APP Pool.
The java script in clients browsers determines the timezone there.

These timezones may be different.

Upvotes: 0

Related Questions