服务器端世界时间(UTC)转换客户端时区时间




当你的服务器是针对世界各地用户提供服务的时候,如果正好你想正确显示服务器的时间。那么你应该获取正确的服务器时间并转化成客户端本地时间的时间在客户端显示与调用。而不应该像“铁路购票系统”一样直接使用客户端的本地时间而出现一系列有趣的BUG。

那么我们应该如何将服务器的协调世界(UTC)时间转换成我们客户端所在时区的正确时间呢?

我们首先应该了解到的时区概念应该是这样的,比如“北京时间”,相对于“世界时间”要正向偏移8个小时0分钟,表示为“+08 00”;假如世界时间是10点,那么我们计算出来的北京时间应该是,世界时间10点+8个小时0分钟=18点。

下来我们看看我们是如何从服务器获取世界时间并转化成本地时间的。

首先,我们应该知道本地时间的时区时间偏移量:即介绍中的“+08 00”。

我们使用JS方法来获得这个值:

</pre><div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); border-image: none; color: rgb(0, 0, 0); overflow: auto; font-family: 'Courier New' !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div class="cnblogs_code_toolbar" style="margin: 5px 0px 0px; padding: 0px; background-color: rgb(245, 245, 245);"><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;"><a target=_blank title="复制代码" style="margin: 0px; padding: 0px; border: currentColor !important; border-image: none !important; color: rgb(0, 0, 0); text-decoration: none; background-color: rgb(245, 245, 245) !important;" href="javascript:void(0);"><img style="margin: 0px; padding: 0px; border: currentColor !important; border-image: none !important; max-width: 900px; background-color: rgb(245, 245, 245) !important;" alt="复制代码" src="https://i-blog.csdnimg.cn/blog_migrate/69c5a8ac3fa60e0848d784a6dd461da6.gif" /></a></span></div><pre style="margin: 0px; padding: 0px; font-family: 'Courier New' !important; font-size: 12px !important; white-space: pre-wrap; -ms-word-wrap: break-word;"> <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">function</span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;"> clientTimeZone() {
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">获得时区偏移量</span>
            <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">var</span> timeOffset = <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">new</span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;"> Date().getTimezoneOffset();
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">获得时区小时偏移基数</span>
            <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">var</span> hour = parseInt(timeOffset / 60<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">);
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">获得时区分钟偏移基数</span>
            <span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">var</span> munite = timeOffset % 60<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">;
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">var</span> prefix = "-"<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">;
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">if</span> (hour < 0 || munite < 0<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">) {
                prefix </span>= "+"<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">;
                hour </span>= -<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">hour;
                </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">if</span> (munite < 0<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">) {
                    munite </span>= -<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">munite;
                }
            }
            hour </span>+= " "<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">;
            munite </span>+= " "<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">;           
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">if</span> (hour.length == 2<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">) {
                hour </span>= "0" +<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;"> hour;
            }
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">if</span> (munite.length == 2<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">) {
                munite </span>= "0" +<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;"> munite;
            }           
            
            </span><span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;">return</span> prefix + hour +<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); line-height: 1.8; font-family: 'Courier New' !important; font-size: 12px !important;"> munite;
        }</span>
复制代码
</pre><p style="margin: 10px auto; padding: 0px; text-indent: 0px;"> </p><pre class="javascript" style="margin: 0px; padding: 0px; white-space: pre-wrap; -ms-word-wrap: break-word;" name="code">

使用上面方法能很方便的获取时区时间偏移值。

接下来我们应该使用ajax或者页面回发的方式调用服务器端方法。基于情况不同,调用服务器端方法略。

我们的服务器端方法首先会先获得当前的“协调世界(UTC)时间”,再同“时区时间偏移值”相加的方式来得到客户端时间。

方法如下:

<span style="margin: 0px; padding: 0px; line-height: 1.8;font-size:14px;">  </span>
复制代码
 /// <summary>
        /// 计算客户端所在时区时间
        /// </summary>
        /// <param name="clientTimeOffset">要传入的时区时间偏移值</param>
        /// <returns>客户端时区时间</returns>
        public DateTime GetClientTime(string clientTimeOffset) 
        {
            //获取协调世界时间
            DateTime utcTime = DateTime.UtcNow;
            //按小时,分钟分割时区偏移值
            string[] timeOffset = clientTimeOffset.Split(' ');
            int hourOffset = int.Parse(timeOffset[0]);
            int minuteOffset = int.Parse(timeOffset[1]);
            //协调世界时间与时区小时偏移值相加
            utcTime = utcTime.AddHours(hourOffset);
            //协调世界时间与时区分钟偏移值相加
            utcTime = utcTime.AddMinutes(minuteOffset);
            return utcTime;
        }
复制代码
 
 

通过GetClientTime方法我们能够正确得到客户端所在时区的时间。调用它并返回它就可以放心在客户端应用了。

 

更多WEB开发技术请加群:Asp.Net高级群 号码:261882616  博主以及同事和你共同探讨感兴趣的话题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值