在MVC视图中使用JavaScript将UTC时间转换为客户端浏览器的时区 [英] Convert UTC time to Client browser's timezone using JavaScript in a MVC View

查看:69
本文介绍了在MVC视图中使用JavaScript将UTC时间转换为客户端浏览器的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个模块,已登录的用户可以在其中发表任何文章的评论.我将 UTC时间保存为数据库中的发布时间.根据我的要求,我需要显示发布时间以进行评论,以及发布时间与当前时间之间的时间差(例如,上次在Facebook中看到的时间或在Facebook中发表评论的时间)与评论.发布的时间必须显示在客户端浏览器的时区中.

I have a module in my website where logged in users can post comments on any article. I am saving UTC time as posted time in database. As per my requirement I need to display posted time for comment and the time difference between posted time and current time (for example last seen in Facebook or comment posted time in Facebook) with the comment. The posted time must show in Client Browser's timezone.

例如:用户XYZ在 01:00 UTC

  • 如果XYZ帐户是从印度登录的,则时区必须为印度时区(UTC + 05:30)和时间将出现在6:30 AM.

  • If account of XYZ is logged in from India then timezone must be India Time Zone (UTC+05:30) and time will appear 6:30 AM.

如果XYZ帐户是从新加坡登录的,则时区必须为新加坡时区(UTC + 8:00)并显示时间9:00 AM.

If account of XYZ is logged in from Singapore then timezone must be Singapore Time Zone(UTC+8:00) and time will appear 9:00 AM.

要实现上述目标,我已经在网上搜索并找到了此帖子从以下位置获取客户端时区浏览器,这对于获取客户端浏览器的时区很有帮助.(我尚无法测试它是否可以正确转换时间)

To achieve above I have searched on web and found this post get client time zone from browser which was very helpful to get client browser's timezone. (Which I am not able to test yet if it will convert the time correctly or not)

现在,主要问题是将UTC时间转换为给定的时区(我正在使用此处提到的解决方案

Now the main issue is to convert UTC time to given timezone (which I am getting using solution mentioned here get client time zone from browser).

请分享建议.

推荐答案

您可以从 JS 的此代码段中获取客户端浏览器的偏移值.

You can get offset value of the client browser from this snippet of JS.

function returnTimeDiff(postDateTime, spanid) {
    var offset = (new Date().getTimezoneOffset() / 60)
}

希望它会对您有所帮助.

Hope it will help you.

这篇关于在MVC视图中使用JavaScript将UTC时间转换为客户端浏览器的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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