Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.net DateTime.Now when server is in a different timezone

Options
  • 24-02-2010 12:20pm
    #1
    Registered Users Posts: 2,791 ✭✭✭


    Hi, have a quick question for ye .net heads out there.

    I use DateTime.Now quite a lot for logging details. When deployed to the webserver which is located in the U.S, the time is 7 hours behind the time that it was actually logged.

    Is there a way of configuring this in web.config similar to the Culture settings?

    Thanks


Comments

  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    I don't think it can be set in web.config, but I'm not 100% sure on that.

    How about just converting the DateTime to the UTC DateTime - would that not give you the current Irish time?


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    eoin wrote: »
    I don't think it can be set in web.config, but I'm not 100% sure on that.

    How about just converting the DateTime to the UTC DateTime - would that not give you the current Irish time?

    Was hoping I could avoid code changes as it's used extensively throughout the web app. You'd think setting the globalization cultier to en-IE would make it automatic..


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The DateTime object has a function UtcNow which gives you the current UTC time.

    Note though that Ireland shifts out of UTC time during the summer and goes to UTC + 1.

    For international deployments you should always log timestamps as UTC and then locally convert this to local time.

    If you really, really want to show the current Irish time, then obtain the UTC time, determine whether or not Ireland is in UTC or UTC + 1 based on the date and modify the timestamp accordingly.

    The DateTime.Now function gives you the current system time, so it will ignore localisation settings. You could change the system time, but that'll probably cause a ****load of problems.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Nice one lads, more work than I have planned but still worth it. Thanks


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    John_Mc wrote: »
    Was hoping I could avoid code changes as it's used extensively throughout the web app. You'd think setting the globalization cultier to en-IE would make it automatic..

    If you're making changes would it be worth you while to implementt this in its own class? You could then use that class to replace the existing DateTime.Now code throughout your app. That way if you need to make further changes you'll only have to do it in one place.


  • Advertisement
  • Registered Users Posts: 1,825 ✭✭✭Gambler


    This article might be of help to you:

    http://www.danrigsby.com/blog/index.php/2008/08/24/timezone-vs-timezoneinfo-in-net/

    Dim some basic playing around with this and it looks like this should return what you want:

    TimeZoneInfo.ConvertTime(Datetime.Now, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"))


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Gambler wrote: »
    This article might be of help to you:

    http://www.danrigsby.com/blog/index.php/2008/08/24/timezone-vs-timezoneinfo-in-net/

    Dim some basic playing around with this and it looks like this should return what you want:

    TimeZoneInfo.ConvertTime(Datetime.Now, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"))

    That's perfect thanks :)


Advertisement