如何反序列化asp.net日期时间为JSON日期时间与客户的时区 [英] How to deserialize asp.net datetime as JSON datetime with client's timezone

查看:258
本文介绍了如何反序列化asp.net日期时间为JSON日期时间与客户的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和日期时间挣扎了一下。我使用asp.net mvc的API控制器,​​Microsoft SQL Server和AngularJS。

I'm struggling with datetimes a bit. I'm using asp.net mvc api controllers, a microsoft sql server and AngularJS.

在某些按钮点击我要送一个JSON格式的日期的API控制器。当我发布2015年11月31日00:00,我期待在菲德勒,看看什么是真正贴,我看到的日期格式为这样:2015-11-30T23:00:00.000Z。 (2015年11月31日 - 1小时UTC + 01:00阿姆斯特丹,柏林,误码率....)这是完美的,因为有可能是和客户机的时区之间的差的sql服务器可能英寸(或者是什么?)

On some button click I'm sending a JSON formatted date to an api-controller. When I post 2015-11-31 00:00 and I look in Fiddler to see what's really posted, I see that the date is formatted as such: 2015-11-30T23:00:00.000Z. (2015-11-31 - 1 hour UTC+01:00 Amsterdam, Berlin, Ber....) This is perfect because there might be a difference between the timezone the sql server might be in and the client. (Or is it?)

问题是,虽然:当我到达的日期从SQL Server回到它没有考虑客户的时区考虑在内。当我读从SQL Server DateTime对象,我返回它JSON格式的,这就是正在显示的日期是:2015-11-30T23:00:00.000Z。我希望它加1小时可在客户端的时区。

The problem is though: When I get the date back from the sql server it doesn't take the client's time zone into account. When I read the DateTime object from the sql server and I return it JSON formatted, the date that's being displayed is: 2015-11-30T23:00:00.000Z. I want it to add 1 hour to be in the timezone where the client is.

我的问题是:我做的就是它保持在时区为帐户反序列化,从我的API控制器回来的JSON字符串做什么

My question is: What do I do to get it to keep the timezone in to account while deserializing the JSON string that comes back from my api-controller?

TIA!

推荐答案

问题原来是该对象被反序列化时,日期属性的不是DateTime类型的。它是字符串类型。通过使用新的日期只需将其转换为日期(2015-11-30T23:00:00.000Z)。将这样的伎俩

Problem turns out to be that when the object is being deserialized, the date property is not of type DateTime. It is of type string. Simply converting it to date by using new Date("2015-11-30T23:00:00.000Z") will do the trick.

我做了过滤器的:

.filter('from_gmt_to_local_date', [function () {
  return function (text) {
    return new Date(text);
  };
}])

用法:

{{contract.StartDate | from_gmt_to_local_date | date:'dd-MM-yyyy'}}

希望这可以帮助任何人。

Hope this helps anybody.

这篇关于如何反序列化asp.net日期时间为JSON日期时间与客户的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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