完成你发布的内容,并将其设为私有似乎对我来说很好.
[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)); } } }
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; }