处理ASP.NET MVC中日期的最佳方法 - Javascript应用程序

时间:2022-04-14 04:47:51

Our application heavilly relies on javascript (jQuery). So there are lots of 'application/json' requests.

我们的应用程序依赖于javascript(jQuery)。所以有很多'application / json'请求。

We've been struggling with dates for some time, but despite our efforts we couldn't figure out how to deal with it in ASP.NET MVC application.

我们一直在努力与日期相关,但尽管我们付出了努力,但我们无法弄清楚如何在ASP.NET MVC应用程序中处理它。

Here what we want:

这就是我们想要的:

  1. Serialize/deserialize DateTime objects on the server side (preferably with DateTimeKind.Utc).
  2. 在服务器端序列化/反序列化DateTime对象(最好使用DateTimeKind.Utc)。

  3. Work with Date object on the client side. Work with it in UTC. Format dates somehow.
  4. 在客户端使用Date对象。在UTC中使用它。格式化日期不知何故。

Here are problems:

这是问题:

  1. Date client object is always with end-user's timezone. JSON.stringify(new Date()) automatically does a shift.

    日期客户端对象始终具有最终用户的时区。 JSON.stringify(new Date())自动进行转换。

    JSON Stringify changes time of date because of UTC

    由于UTC,JSON Stringify更改日期时间

    This question helper a bit, but we need to remember to shift dates before sending it to the server. We also need to remember that we need a shift once we do new Date('2012-10-19T10:23:47.778Z') We know that JSON is not aware of dates, but it would be nice to have analogue of JSON.parse that would parse dates.

    这个问题有点帮助,但我们需要记住在将日期发送到服务器之前更改日期。我们还需要记住,一旦我们做了新的日期,我们需要一个班次('2012-10-19T10:23:47.778Z')我们知道JSON不知道日期,但是拥有JSON的模拟会更好。解析将解析日期。

  2. We use Newton Json.net to serialize/deserialize dates, but it does it in DateTimeKind.Unspecified

    我们使用Newton Json.net来序列化/反序列化日期,但它在DateTimeKind.Unspecified中执行

Question:

Is there an elegant way to work with dates both on the server and client? We need it as a cross-cutting thing in the whole application.

是否有一种优雅的方式来处理服务器和客户端上的日期?我们需要它作为整个应用程序中的交叉事物。

UPD:

I had to put it more clearly. In our application we don't need take into account client time zones since we operate only with dates.

我不得不说得更清楚。在我们的应用程序中,我们不需要考虑客户端时区,因为我们只使用日期。

So if server returns 2012-10-19T10:23:47.778Z', we need Date object:

因此,如果服务器返回2012-10-19T10:23:47.778Z',我们需要Date对象:

Fri Oct 19 2012 10:23:47 GMT+0300 (Kaliningrad Standard Time)

2012年10月19日星期五10:23:47 GMT + 0300(加里宁格勒标准时间)

Not

Fri Oct 19 2012 13:23:47 GMT+0300 (Kaliningrad Standard Time) (Date is shifted)

2012年10月19日星期五13:23:47 GMT + 0300(加里宁格勒标准时间)(日期转移)

2 个解决方案

#1


4  

I would strongly suggest reading following post:

我强烈建议阅读以下帖子:

Which leads to general conclusion that you should use ISO dates:

这导致了您应该使用ISO日期的一般结论:

  • By using modern browser .toJSON() method on client side
  • 通过在客户端使用现代浏览器.toJSON()方法

  • By properly configuring JSON serialization on server side (JsonNetFormatter with IsoDateTimeConverter)
  • 通过在服务器端正确配置JSON序列化(带有IsoDateTimeConverter的JsonNetFormatter)

#2


1  

Probably the best way is to send a time value as milliseconds since 1970-01-01T00:00:00Z (i.e. UTC), then you can just do new Date(timeValue) and there's your date object set to that UTC time. It will return date and time based on the client system time zone setting.

可能最好的方法是自1970-01-01T00:00:00Z(即UTC)发送时间值为毫秒,然后您可以只执行新的Date(timeValue)并将日期对象设置为该UTC时间。它将根据客户端系统时区设置返回日期和时间。

e.g. for me:

例如为了我:

alert(new Date(0)); // Thu Jan 01 1970 10:00:00 GMT+1000 (EST)

If you mess with that date object on the client (say add a day) you can just send back the time value:

如果您在客户端上混淆了该日期对象(比如添加一天),您只需发回时间值:

var timeValue = 1350518400000; // 2012-20-18T00:00:00Z
var date = new Date(timeValue);
alert(date);                   // 2012-10-18T10:00:00UTC+1000

// add a day
date.setDate(date.getDate() + 1); 
alert(date.getTime());         // 1350604800000

Subtract the first timeValue from the second and you get 86400000, which is the milliseconds in one day (it will be an hour either way if the day crosses a daylight saving change boundary). Do what you like with the time value at the sever, keep it as a number or convert it to a date string.

从第二个时间减去第一个timeValue,得到86400000,这是一天内的毫秒数(如果当天越过夏令时变化边界,它将是一个小时)。使用服务器上的时间值执行您喜欢的操作,将其保留为数字或将其转换为日期字符串。

#1


4  

I would strongly suggest reading following post:

我强烈建议阅读以下帖子:

Which leads to general conclusion that you should use ISO dates:

这导致了您应该使用ISO日期的一般结论:

  • By using modern browser .toJSON() method on client side
  • 通过在客户端使用现代浏览器.toJSON()方法

  • By properly configuring JSON serialization on server side (JsonNetFormatter with IsoDateTimeConverter)
  • 通过在服务器端正确配置JSON序列化(带有IsoDateTimeConverter的JsonNetFormatter)

#2


1  

Probably the best way is to send a time value as milliseconds since 1970-01-01T00:00:00Z (i.e. UTC), then you can just do new Date(timeValue) and there's your date object set to that UTC time. It will return date and time based on the client system time zone setting.

可能最好的方法是自1970-01-01T00:00:00Z(即UTC)发送时间值为毫秒,然后您可以只执行新的Date(timeValue)并将日期对象设置为该UTC时间。它将根据客户端系统时区设置返回日期和时间。

e.g. for me:

例如为了我:

alert(new Date(0)); // Thu Jan 01 1970 10:00:00 GMT+1000 (EST)

If you mess with that date object on the client (say add a day) you can just send back the time value:

如果您在客户端上混淆了该日期对象(比如添加一天),您只需发回时间值:

var timeValue = 1350518400000; // 2012-20-18T00:00:00Z
var date = new Date(timeValue);
alert(date);                   // 2012-10-18T10:00:00UTC+1000

// add a day
date.setDate(date.getDate() + 1); 
alert(date.getTime());         // 1350604800000

Subtract the first timeValue from the second and you get 86400000, which is the milliseconds in one day (it will be an hour either way if the day crosses a daylight saving change boundary). Do what you like with the time value at the sever, keep it as a number or convert it to a date string.

从第二个时间减去第一个timeValue,得到86400000,这是一天内的毫秒数(如果当天越过夏令时变化边界,它将是一个小时)。使用服务器上的时间值执行您喜欢的操作,将其保留为数字或将其转换为日期字符串。