There is a difference between local and server time
Asked Answered
H

4

8

In MVC4 C# application I used DateTime.Now when object created in my application. After deploying, there was +8 hours difference between local computer and hosting. Then I changed it to DateTime.UtcNow, now difference is -4 hours.

For example in my computer date is 20 Mar 2013, 14:28:12, but date stores to database like 20 Mar 2013, 10:28:12.

What should I do all user in all countries to use 1 same date?

Edit: my time zone is (UTC+04:00) Baku, server is following PST

Hypoxanthine answered 20/3, 2013 at 10:41 Comment(5)
This is one for @JonSkeet, he loves date time questions :)Portal
Hmm. Not to be the guy who asks if it's plugged in, but: Is the date/time on the server correct? At least, does it match the one on your box?Galloot
ALSO: Which timezone is the server in, and which timezone are you in?Galloot
Hosting is Arvixe, I dont think theirs time is uncorrectHypoxanthine
I am in Azerbaijan, timezone is (UTC+04:00) Baku and server is Arvixe. I do not now that time zone exactly. I knew DateTime.Now always takes server's date/timeHypoxanthine
S
4

Let say for example that your user is in New York (GMT - 5) and your server is in India (GMT + 5:30).

When the user sees a "local" time of 10:00am (EST), your server would see a "local" time of 7:30pm (IST). However, by using UTC in both places....the user would see a UTC time of 3pm and the server would see a UTC time of 3pm.

Using UTC in all places but keeping a referece of the users time zone allows you to always be working in UTC but still convert the times into the users "local" time when you want to show it to them.

You also cannot rely on where a server is hosted especically if it is a virtual server. Nor can you assume what time zone the hosting company set their servers to. Best to always code defensivly by using DateTime.UtcNow when storing date times in the database.

To convert this UTC time back to the time zone of a user you will need to do the following:

DateTime utcTime = new DateTime(2013, 03, 25, 10, 20, 00);
TimeZoneInfo usersTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime usersLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, usersTimeZone);
Console.WriteLine(utcTime.ToString("hh:mm:ss");
Console.WriteLine(usersLocalTime.ToString("hh:mm:ss");

FYI, you can find a list of the timezones from:

TimeZoneInfo.GetSystemTimeZones()

This should print out:

10:20:00
05:20:00

As you can see, you will need to know the users time zone. There are 2 ways to know the users time:

  1. Have the user select their timezone in their user preferences
  2. Use some javascript to detect the timezone of their machine and post it back with the form

I would suggest the first option there as the second one can make things more difficult for people. For example, if they are UK based and fly to the states for the week. Everything will change zones and they may not realise!

Suffolk answered 20/3, 2013 at 10:55 Comment(3)
I saved dates like DateTime.UtcNow. Now, DateTime.UtcNow.ToLocalTime() not working right.Hypoxanthine
Are you trying to convert the times back after getting back from the database? If so, the DateTime object doesn't know what the local time of the user was. It will be using the local time of the server. I will update my answer to add in the extra detail on what you will need to do to get the original time back...Suffolk
thanks for it, I understand that, I can do it only with javascript. For 10 day in server side I type some codes like ToLocalTime() etc. to show date/time with user local time. So, It is not right as You say. thanks for it. Can I manage it with globalization (culture and uiCulture) ?Hypoxanthine
W
2

when i was working on a project used by international users, we would always use DateTime.UtcNow

if it a local project - ie only designed for uses in your timezone then DateTime.Now will be sufficient.

Wagram answered 20/3, 2013 at 10:48 Comment(0)
M
1

Your local time is your system time. Check the timezone in your operating system. The server time you are referring is according to the timezone in your server configuration files.

Make sure you have operating system timezone and your server timezone similar.

Modicum answered 20/3, 2013 at 10:48 Comment(3)
Assume that my local time and server time is similar, if users are in other countries? theirs timezone is differentHypoxanthine
You can detect users timezone and display time in their local timezone. Suppose I am in India, my local time is 5.00 PM. Server is in Ottawa, Canada and server time is 7:30 AM. The time stored on server will be server time. When displaying to user it should be converted to local time.Modicum
Ok, I understand. i changed all DateTime.UtcNow and will convert it before displaying. thanksHypoxanthine
K
0

After reading all the answers and comments I have did something like this with node express.

In my case computer time is 11/7/2023 03:42:39 PM but date store in DB like 11/7/2023 12:12:39 PM

while storing date in the database (Postgres) I used toUTCString() method to convert date into UTC format

let serverTime = new Date().toUTCString()
//Here serverTime is stored as UTC format in DB

then using the moment library from npm, convert UTC format into local timezone

const moment = require('moment');

const localDateTime = moment.utc(serverTime).local().format('MMMM DD YYYY HH:mm:ss');

log serverTime

Tue, 11 Jul 2023 09:53:40 GMT // In DB store like this

log localDateTime

July 11 2023 15:23:40 // retrieve like this
Kane answered 11/7, 2023 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.