-1

I need to get users timezone and display datetime according to user timezone. If a user save some data a log will maintain in database and user can see the log but I need to show datetime according to location where user is loggedIn.

Hasnain Bukhari
  • 458
  • 2
  • 6
  • 23

1 Answers1

1

A commonly used scheme to achieve this would be to store all dates in UTC and then convert to local time zone in client code (javacsript/jquery/whatever other javascript framework you want to use).

A simple controller action demonstrating saving a utc date:

[HttpPost]
public ActionResult SaveDate()
{
  try
  {
    _dateRepository.Save(DateTime.UtcNow);

    return Index(date);
  }
  catch (Exception ex)
  {
    _log.Error(ex.Message, ex);
  }
}

and then on load of the page where you want to display the data, you can have a script to convert the time to a local timezone as detailed here:

var localDateTime = utcDateTime.ToString();

You would then display the javascript-converted data to the user. This allows you to save all dates in a uniform format and not have to worry about keeping track of timezone information.

GregH
  • 5,125
  • 8
  • 55
  • 109