DataContractJsonSerializer And DateTime
用DataContractJsonSerializer 对DateTime进行序列化时,采用的是对DateTime.Ticks进行UNIX TIMESTAMPE模式的转换。
需要注意的是这里用毫秒,如果用PHP等语言进行解析时需要进行除1000的转换。如:
echo time(); //php下获取Unix epoch time
echo date('Y-m-d H:i:s',1258698799374/1000+60*60*8); //php下转换epoch time
echo date('Y-m-d H:i:s',1258698799374/1000+60*60*8); //php下转换epoch time
顺便贴上 DataContractJsonSerializer 的使用:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ToJson(DateTime.Parse("2009-11-20")));
Console.WriteLine(ToJson(DateTime.Now.Ticks));
Console.ReadKey();
}
public static string ToJson(object value)
{
var ser = new DataContractJsonSerializer(value.GetType());
var ms = new MemoryStream();
ser.WriteObject(ms, value);
return Encoding.Default.GetString(ms.ToArray());
}
}
static void Main(string[] args)
{
Console.WriteLine(ToJson(DateTime.Parse("2009-11-20")));
Console.WriteLine(ToJson(DateTime.Now.Ticks));
Console.ReadKey();
}
public static string ToJson(object value)
{
var ser = new DataContractJsonSerializer(value.GetType());
var ms = new MemoryStream();
ser.WriteObject(ms, value);
return Encoding.Default.GetString(ms.ToArray());
}
}
ref:http://www.epochconverter.com/#
posted on 2009-11-20 14:42 flyingchen 阅读(847) 评论(0) 编辑 收藏 举报