如何将日期时间从 JSON 转换为 C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/249721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert DateTime from JSON to C#?
提问by Mark Ingram
Possible Duplicate:
How to convert UNIX timestamp to DateTime and vice versa?
I've got the following class:
我有以下课程:
[DataContractAttribute]
public class TestClass
{
[DataMemberAttribute]
public DateTime MyDateTime { get; set; }
}
Here's the JSON:
这是JSON:
{ "MyDateTime":"1221818565" }
The JSON is being returned from a PHP webservice.
JSON 正从 PHP 网络服务返回。
What I need to do, is convert that epoch string into a valid C# DateTime. What's the best way of doing this?
我需要做的是将该纪元字符串转换为有效的 C# DateTime。这样做的最佳方法是什么?
I can do this:
我可以做这个:
[IgnoreDataMemberAttribute]
public DateTime MyDateTime { get; set; }
[DataMemberAttribute(Name = "MyDateTime")]
public Int32 MyDateTimeTicks
{
get { return this.MyDateTime.Convert(...); }
set { this.Created = new DateTime(...); }
}
But the trouble with this is, the MyDateTimeTicks is public (changing it to private causes an exception in the serialization process)
但问题是,MyDateTimeTicks 是公开的(将其更改为私有会导致序列化过程中出现异常)
采纳答案by TheSoftwareJedi
Finishing what you posted, AND making it private seemed to work fine for me.
完成您发布的内容并将其设为私有似乎对我来说效果很好。
[DataContract]
public class TestClass
{
private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
[IgnoreDataMember]
public DateTime MyDateTime { get; set; }
[DataMember(Name = "MyDateTime")]
private int MyDateTimeTicks
{
get { return (int)(this.MyDateTime - unixEpoch).TotalSeconds; }
set { this.MyDateTime = unixEpoch.AddSeconds(Convert.ToInt32(value)); }
}
}
回答by Dan Esparza
Here's what I've come up with. In C#, it looks like you need to create a new DateTime and add the epoch value as 'seconds' to this DateTime. Here's what it looks like in code:
这是我想出的。在 C# 中,您似乎需要创建一个新的 DateTime 并将纪元值作为“秒”添加到此日期时间。下面是它在代码中的样子:
new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(1221818565);
When using the Visual Studio immediate window, I printed the result of this operation to the debugger console:
在使用 Visual Studio 即时窗口时,我将这个操作的结果打印到调试器控制台:
{9/19/2008 10:02:45 AM}
Date: {9/19/2008 12:00:00 AM}
Day: 19
DayOfWeek: Friday
DayOfYear: 263
Hour: 10
Kind: Unspecified
Millisecond: 0
Minute: 2
Month: 9
Second: 45
Ticks: 633574153650000000
TimeOfDay: {10:02:45}
Year: 2008
回答by ageektrapped
What you want is the following:
你想要的是以下内容:
DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
DateTime dotnetTime = unixEpoch.AddSeconds(Convert.ToDouble(ticks));
where ticks
is the value passed to you by PHP.
ticks
PHP 传递给您的值在哪里。
回答by Jeremy
I know your question was for PHP, but I just wanted to note a "gotcha" for .NET JSON: it appears that .NET gives you the date in "milliseconds since epoch" (as opposed to seconds). In this case, the AddSeconds line should be:
unixEpoch.AddMilliseconds(Int64.Parse(date));
我知道您的问题是针对 PHP 的,但我只是想指出 .NET JSON 的一个“问题”:看来 .NET 以“自纪元以来的毫秒数”(而不是秒)为您提供日期。在这种情况下,AddSeconds 行应该是:
unixEpoch.AddMilliseconds(Int64.Parse(date));
More info: http://blogs.msdn.com/b/marcelolr/archive/2008/03/05/system-datetime-ticks-vs-json-date.aspx
更多信息:http: //blogs.msdn.com/b/marcelolr/archive/2008/03/05/system-datetime-ticks-vs-json-date.aspx
回答by yossi
private DateTime ConvertJsonStringToDateTime(string jsonTime)
{
if (!string.IsNullOrEmpty(jsonTime) && jsonTime.IndexOf("Date") > -1)
{
string milis = jsonTime.Substring(jsonTime.IndexOf("(") + 1);
string sign = milis.IndexOf("+") > -1 ? "+" : "-";
string hours = milis.Substring(milis.IndexOf(sign));
milis = milis.Substring(0, milis.IndexOf(sign));
hours = hours.Substring(0, hours.IndexOf(")"));
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(milis)).AddHours(Convert.ToInt64(hours) / 100);
}
return DateTime.Now;
}