存储UTC时间。在当地时间显示

时间:2021-11-03 15:57:49

Im writing a mvc4 application that will be used globally. Part of the application is recording when a transaction was added or modified.

我正在编写一个将在全球范围内使用的mvc4应用程序。部分应用程序是在添加或修改事务时进行记录。

So I am saving the transaction datetime as UTC. from the clientside whats my best way to display the date as they are expecting?

所以我将事务日期时间保存为UTC。从客户端看我最好的方式显示他们期待的日期?

Is this a javascript function I should be using or should I be doing something within the view?

这是我应该使用的javascript函数还是应该在视图中执行某些操作?

2 个解决方案

#1


0  

Internally, javascript date objects store the time as a number of milliseconds since an epoch UTC (1970-01-01T00:00:00Z). The simplest way to transfer time is using that number, so:

在内部,javascript日期对象将时间存储为自纪元UTC(1970-01-01T00:00:00Z)以来的毫秒数。转移时间的最简单方法是使用该数字,因此:

// 2012-09-16T10:30:00.000Z
var ms = 1347791400000;

// Create a new date object using UTC milliseconds
var d = new Date(ms);

alert(d); // Shows local time equivalent, e.g. 2012-09-16T20:30:00GMT+1000

Alternatively, you can pass a UTC timestamp, split it into its components and convert it to a date object using Date.UTC. Note that months are zero based so one must be subtracted from the calendar month number.

或者,您可以传递UTC时间戳,将其拆分为其组件并使用Date.UTC将其转换为日期对象。请注意,月份为零,因此必须从日历月份数中减去一个月份。

To get the UTC millisecond value for a local time, use Date.prototype.getTime. For the example above:

要获取本地时间的UTC毫秒值,请使用Date.prototype.getTime。对于上面的例子:

alert(d.getTime()); // 1347791400000

#2


0  

It depends on your preference. If you have user time zone on the server side, stored in the database, the simplest way is to use DateTimeOffset.

这取决于您的偏好。如果服务器端有用户时区,存储在数据库中,最简单的方法是使用DateTimeOffset。

Example: Creating a DateTime in a specific Time Zone in c# fx 3.5.

示例:在c#fx 3.5中的特定时区中创建日期时间。

Alternatively, use JavaScript getTimezoneOffset() Method, set cookie and read it on server. Also you can use many client libs, especially if you use client templating.

或者,使用JavaScript getTimezoneOffset()方法,设置cookie并在服务器上读取它。您还可以使用许多客户端库,尤其是在使用客户端模板时。

#1


0  

Internally, javascript date objects store the time as a number of milliseconds since an epoch UTC (1970-01-01T00:00:00Z). The simplest way to transfer time is using that number, so:

在内部,javascript日期对象将时间存储为自纪元UTC(1970-01-01T00:00:00Z)以来的毫秒数。转移时间的最简单方法是使用该数字,因此:

// 2012-09-16T10:30:00.000Z
var ms = 1347791400000;

// Create a new date object using UTC milliseconds
var d = new Date(ms);

alert(d); // Shows local time equivalent, e.g. 2012-09-16T20:30:00GMT+1000

Alternatively, you can pass a UTC timestamp, split it into its components and convert it to a date object using Date.UTC. Note that months are zero based so one must be subtracted from the calendar month number.

或者,您可以传递UTC时间戳,将其拆分为其组件并使用Date.UTC将其转换为日期对象。请注意,月份为零,因此必须从日历月份数中减去一个月份。

To get the UTC millisecond value for a local time, use Date.prototype.getTime. For the example above:

要获取本地时间的UTC毫秒值,请使用Date.prototype.getTime。对于上面的例子:

alert(d.getTime()); // 1347791400000

#2


0  

It depends on your preference. If you have user time zone on the server side, stored in the database, the simplest way is to use DateTimeOffset.

这取决于您的偏好。如果服务器端有用户时区,存储在数据库中,最简单的方法是使用DateTimeOffset。

Example: Creating a DateTime in a specific Time Zone in c# fx 3.5.

示例:在c#fx 3.5中的特定时区中创建日期时间。

Alternatively, use JavaScript getTimezoneOffset() Method, set cookie and read it on server. Also you can use many client libs, especially if you use client templating.

或者,使用JavaScript getTimezoneOffset()方法,设置cookie并在服务器上读取它。您还可以使用许多客户端库,尤其是在使用客户端模板时。

相关文章