Home > Mobile >  Change DateTime to correspond user local after receiving data from Azure database
Change DateTime to correspond user local after receiving data from Azure database

Time:04-18

I have a service that is writing data into Azure database. However Azure automatically writes these "TimeStamps" in UTC. Due to I am not in UTC zone, my TimeStamps are not correct. After some investigations, I understood that there are several options. I am having hard times to understand which one should I use? Currently it is hard to see all possible problems that may appear in the future.

Here is my model:

  public class DataRecord : IDataRecord
  {
    public int Id { get; set; }
    public DateTime TimeNow { get; set; }
  }

So currently Azure don't care in which zone I am and just writes this DateTime time in UTC.

Possible options (I understood) I have:

  1. Change DateTime into string and then parse back into DateTime on Client side
  2. Add settings on Client side to select TimeZone, then do some math Azure DateTime offset by selected TimeZone. If this solution, in which format should I create a list of time zones and how to perform calculations? My TimeStamp in UTC in DateTime format ?
  3. Retrieve clients time automatically, I have seen there are some JavaScript solutions available that are capable to get user browser time. For example: enter image description here

    Note: Here you might encounter one issue, if your server time is not same as your local time in that case time conversion may not be as expected. So if that is the case you could have a try below way:

    Convert Azure Time To Any Standard Time:

    This the way you could convert UTC time to any standard time zone. Here is the example:

            var azureTime = "18-Apr-22 5:21:29 AM"; // Getting Time From Azure 
            var convertedAzureTime = Convert.ToDateTime(azureTime); // Because I have save date time as string
            TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
            DateTime yourLocalTime = TimeZoneInfo.ConvertTimeFromUtc(convertedAzureTime, cstZone);
            Console.WriteLine("Azure Time {0}",convertedAzureTime);
            Console.WriteLine("Converted Local Time {0}",yourLocalTime);
    

    Note: You can convert in anytime zone you want by using enter image description here

    Hope above explanation and example would be guided you accordingly

  • Related