琐事:如何将JSON2.org DateTime字符串转换为C#DateTime [英] Trivia: How to convert a JSON2.org DateTime string to C# DateTime

查看:111
本文介绍了琐事:如何将JSON2.org DateTime字符串转换为C#DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Asp.Net MVC 2期货似乎没有处理JSON DateTime(包括双精度值和十进制值)。因此,我将所有输入设置为字符串,使用数据验证,并且事情工作得很好。

Asp.Net MVC 2 Futures doesn't seem to handle JSON DateTime well (including double and decimal values). As such, I setup all inputs as string, used Data Validation, and things worked pretty well.

但是,我有这个JSON2.js日期从Firefox 3.6: p>

However, I have this JSON2.js date from Firefox 3.6:

"/Date(1288296203190)/"

如何将其转换为C#中的有效日期?

How do I turn this in to a valid date in C#?

var a = new DateTime(1288296203190);

没有给正确的日期(1/2/0001 11:47:09 AM)而不是Thu Oct 28 2010 16:03:23 GMT-0400(Eastern Daylight Time)。这可能是因为32位整数只有10位数。但是,这也失败了:

That doesn't give the right date (1/2/0001 11:47:09 AM) instead of Thu Oct 28 2010 16:03:23 GMT-0400 (Eastern Daylight Time). It's probably because a 32 bit integer is only 10 digits. However, this fails too:

var a = Int64.Parse("1288296203190");
var b = new DateTime(a);

b的值是1/2/0001 11:47:09 AM。

b's value is 1/2/0001 11:47:09 AM.

它做了什么?包?这是某种时间旅行签名位问题吗?

What did it do? Wrap? Is this some kind of time travel "signed bit" issue?

推荐答案

问题是时代的差异。看起来像JSON2.js的日期你使用unix纪元(1970年1月1日)以毫秒为单位。来自System.DateTime(long ticks)的文档说明:

The issue is the difference in epoch. Looks like the JSON2.js date you have uses the unix epoch (January 1, 1970) measured in ms. From the System.DateTime(long ticks) documenttion:


expects以已经过去的100纳秒间隔数表示的日期和时间从0001年1月1日起,在公历中的00:00:00.000。

expects A date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

这样的东西应该可以让你满意。

Something like this should get you what you want.

var unixEpoch = DateTime(1970, 1, 1);
var ticksSinceEpoch = 1288296203190 * 10000;
var time = new DateTime(unixEpoch.Ticks + ticksSinceEpoch);

这篇关于琐事:如何将JSON2.org DateTime字符串转换为C#DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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