简体   繁体   English

使用c#在客户端转换为本地时间

[英]Converting to localtime at client with c#

I have a database with records stored in UTC. 我有一个包含以UTC格式存储的记录的数据库。 The c# .NET application reads the data and populates a *.cshtml table server-side. c#.NET应用程序读取数据并填充* .cshtml表服务器端。 The server doesn't know the client's timezone as it is not a parameter reported by the browser, and there is no login or user form for the user to enter a profile and declare their timezone. 服务器不知道客户端的时区,因为它不是浏览器报告的参数,并且没有用户输入配置文件并声明其时区的登录或用户表单。 The result is a table displayed in UTC at the browser. 结果是在浏览器中以UTC显示的表。 I am aware that if the value is in a javascript variable then it is trivial to get the browser to make timezone offset adjustment. 我知道如果值在javascript变量中,那么让浏览器进行时区偏移调整是微不足道的。 However, the data reaches the browser as a set of TD tags with text between. 但是,数据作为一组带有文本的TD标签到达浏览器。

Any solution I can think of to convert this to local time is inelegant - using javascript to walk through the table, get the innerHTML, parse it, adjust it, and put it back. 我能想到的将其转换为本地时间的任何解决方案都是不优雅的 - 使用javascript遍历表格,获取innerHTML,解析它,调整它并将其放回去。 That feels like an awful solution. 这感觉就像一个糟糕的解决方案。 Is there a better way? 有没有更好的办法? Pass the data as a JSON object and dynamically create the table via javascript? 将数据作为JSON对象传递并通过javascript动态创建表? again it feels unwieldy. 再次感到笨拙。 Is there a simpler way to make the browser take the values between the TD> tags and adjust, like 是否有一种更简单的方法可以使浏览器获取TD>标签之间的值并进行调整,例如

<TD> onsomething="this.innerHTML=adjustTime("2017-06-17 09:00:00");> </TD>

or a better way? 还是更好的方法?

A general rule when dealing with times in different zones is to store & transmit the value in UTC and then allow the 'view' to display it as appropriate ie in the users current timezone. 处理不同区域中的时间时的一般规则是以UTC格式存储和传输值,然后允许“视图”在适当时显示它,即在用户当前时区中显示。

I would add an API that provides a list of times in UTC, then allow the javascript client to render and display them as appropriate 我会添加一个API,它提供UTC的时间列表,然后允许javascript客户端呈现并显示它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM