C#UTC到用户本地时间 [英] C# UTC to Users Local Time

查看:151
本文介绍了C#UTC到用户本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,用户可以上岗。用户可以从全球各地,所以当他们发布,我存储发布日期DateTime.UtcNow。我使用jQuery前段时间插上展现DAT发布类似堆栈溢出(1小时前等...),但我不知道如何转换成我已经存储在系统中对用户本地时间日期?下面是我使用的:

I have a site where users can make posts. The users can be from all over the globe so when they post, I am storing the posted date as DateTime.UtcNow. I am using the JQuery time ago plug in to show the posted dat similar to stack overflow (1 min ago etc...) But I am not sure how to convert to date I have stored in the system to the users local time? Here is what I am using:

public static MvcHtmlString ConvertToLocalTime(this HtmlHelper htmlHelper, DateTime date)
    {
        DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(date.ToString()),DateTimeKind.Utc);
        return MvcHtmlString.Create(convertedDate.ToLocalTime().ToString());
    }

这看到的时间转换为服务器的本地时间,但我需要它的用户的本地时间。我究竟做错了什么?我使用.NET MVC 2和.NET 4.0

This sees to convert the time to the servers local time but I need it for the users local time. What am I doing wrong? I am using .NET MVC 2 and .NET 4.0

推荐答案

在一般情况下,你不会知道在服务器端用户的确切时间区。代替试图转换到用户的本地时间在服务器端,发送的时间以UTC客户机并让客户端转换的时间。该TimeAgo插件自动执行此操作,只要你给它充分ISO8601格式的时间:

In general, you aren't going to know the user's exact time zone on the server side. Instead of trying to convert to the user's local time on the server side, send the time to the client in UTC and have the client convert the time. The TimeAgo plugin does this automatically as long as you give it the time in full ISO8601 format:

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

以Z 在时间戳的结尾是非常重要的;这意味着UTC。

The Z at the end of the timestamp is important; that means UTC.

这篇关于C#UTC到用户本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆