8

I'm developing an application in Visual Studio using C#. When I create a new instance of an object I want to attach the current date and time to it. Using DateTime.Now the time is off by one hour.

I presume its a time-zone or daylight saving issue. I tried calling System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() right before DateTime.Now but got the same result. How do I resolve this?

Paulie22
  • 111
  • 1
  • 1
  • 9
  • please have a look here http://stackoverflow.com/questions/20776093/datetime-now-and-culture-timezone-specific – Christos May 17 '14 at 14:27
  • DateTime.Now returns the current *local* time based on the culture of the machine. What type of date are you trying to get? Local? UTC? – James May 17 '14 at 14:34
  • [`DateTime.Now` gets a DateTime object that is set to the current date and time on this computer, expressed as the local time](http://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx). How do you obtain the value and what makes you think it is incorrect? – CodeCaster May 17 '14 at 14:41
  • Its a website application that allows you to add updates. All updates can be displayed as a list which shows the time/date the update was added. When I add a new update the time is off by one hour. – Paulie22 May 17 '14 at 15:29
  • Ok I've tried running the application on the local host and there's no problem. Th issue is obviously with the server. I'm using Windows Azure. – Paulie22 May 17 '14 at 15:41
  • 2
    Then it sounds to me like you'll need to give up on using local time. Use UTC internally, and convert it to a specific time zone (*not* the server's configured system time zone) when you want to display it? –  May 17 '14 at 15:55
  • on azure, do you have your own dedicated machine where you have access to the time settings in windows, or is it a shared environment? – Ctznkane525 Jan 05 '18 at 21:03

1 Answers1

12

You should use DateTime.UtcNow instead of DateTime.Now to avoid time zone issues (DateTime.Now uses the time zone of the server), then you can convert the UTC value to the user's time zone.

gbellmann
  • 1,945
  • 1
  • 22
  • 27