Convert server UTC time to client local time

◇◆丶佛笑我妖孽 提交于 2019-12-06 06:29:36

Description

You can convert a UTC DateTime to local Time using TimeZoneInfo

Sample

TimeZoneInfo.ConvertTimeFromUtc(YourDateTime, TimeZoneInfo.Local);

You can convert a UTC DateTime to any timezone, if you know the name. For example.

TimeZoneInfo.ConvertTimeFromUtc(YourDateTime, 
                TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));

More Information

create a class
public class TimeConverter
    {
        public static DateTime ConvertToLocalTime(DateTime utcTime, string timeZoneId)
        {
            if (string.IsNullOrEmpty(timeZoneId))
            {
                return utcTime;
            }
            return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcTime, timeZoneId);
        }
}

In controller use TimeConverter

TimeConverter.ConvertToLocalTime(Date, yourTimeZone));

I don't know what your structure is, but if you can convert it to standard time string, DateTime class will parse it. Then simply use the ToLocalTime method.

DateTime time = DateTime.Parse(FILETIME.ToString());
time.ToLocalTime();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!