How to show a UTC time as local time in a webpage?

后端 未结 5 1687
长发绾君心
长发绾君心 2020-12-14 03:32

I have a database that holds a time as UTC. This time can be shown in a webpage, so I’ve been asked to show it as local time in the page as it can be viewed from any country

5条回答
  •  庸人自扰
    2020-12-14 03:38

    EDIT:

    Thinking about it I don't think it is possible to display the local time once you have the client’s culture. I'd certainly be interested to see your colleagues suggestion.

    Getting the US culture, for example, won't help as the US has many timezones.

    I think using Javascript like Anthony suggests would be the way to go...

    OLD:

    You can override the InitializeCulture() method with code that sets the current (chosen or browser reporting) cultures:

    Thread.CurrentThread.CurrentCulture = 
                CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new 
                CultureInfo(selectedLanguage);
    

提交回复
热议问题