c# today's local date RRS feed

  • Question

  • User379720387 posted

    My site is hosted on Azure. All my transactions are saved to the transactions table with a datetime stamp based on DateTime.Now.

    In a certain query I need pass today's date as a parameter, however I need to pass the date in the local time zone.

    For instance currently it is 10:20 PM EST local, therefore it is 2:20 AM UTC

    I have a table timezones, and the timezone description for each user.

    How do I get the local date using c#?

    I am using SQL CE therefore GETUTCDATE() is not available.

    Tuesday, June 2, 2015 10:23 PM

All replies

  • User-821857111 posted

    You can use TimeZoneInfo.ConvertTimeFromUtc to get the current time (and date) for a given timezone:

    var localTimeZoneId = "China Standard Time";
    var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(localTimeZoneId);
    Console.WriteLine("Current China Standard Time: {0}", TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, localTimeZone));

    Wednesday, June 3, 2015 2:17 AM
  • User-376018714 posted


    one thumb rule when dealing with dates is to store UTC stamp in the database. so using C# when you are inserting date just do DateTime.Now.ToUniversalTime().

    so whenever you have to work with that date  field - always convert the date parameter to universal. In that way you are always on the same baseline when it comes to date stored and date that needs to be checked. If you do not want the time part - then just store the datepart. Storing in UTC is needed when you need the time part also in your logic.

    hope this helps.  

    Wednesday, June 17, 2015 8:36 AM