DateTimeConverter.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:2k
- using System;
- namespace AjaxPro
- {
- /// <summary>
- /// Provides methods to serialize and deserialize a DateTime object.
- /// </summary>
- public class DateTimeConverter : IJavaScriptConverter
- {
- public DateTimeConverter() : base()
- {
- }
- public override object Deserialize(IJavaScriptObject o, Type t)
- {
- if(!(o is JavaScriptObject))
- throw new NotSupportedException();
- JavaScriptObject ht = (JavaScriptObject)o;
- int Year = (int)JavaScriptDeserializer.Deserialize(ht["Year"].ToString(), typeof(int));
- int Month = (int)JavaScriptDeserializer.Deserialize(ht["Month"].ToString(), typeof(int));
- int Day = (int)JavaScriptDeserializer.Deserialize(ht["Day"].ToString(), typeof(int));
- int Hour = (int)JavaScriptDeserializer.Deserialize(ht["Hour"].ToString(), typeof(int));
- int Minute = (int)JavaScriptDeserializer.Deserialize(ht["Minute"].ToString(), typeof(int));
- int Second = (int)JavaScriptDeserializer.Deserialize(ht["Second"].ToString(), typeof(int));
- int Millisecond = (int)JavaScriptDeserializer.Deserialize(ht["Millisecond"].ToString(), typeof(int));
- DateTime d = new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
- d = d.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d).TotalMinutes);
- return d;
- }
- public override string Serialize(object o)
- {
- if(!(o is DateTime))
- throw new NotSupportedException();
-
- DateTime dt = Convert.ToDateTime(o);
- dt = dt.ToUniversalTime();
- return String.Format("new Date(Date.UTC({0},{1},{2},{3},{4},{5},{6}))",
- dt.Year,
- dt.Month -1,
- dt.Day,
- dt.Hour,
- dt.Minute,
- dt.Second,
- dt.Millisecond);
- }
- public override Type[] SerializableTypes
- {
- get
- {
- return new Type[]{typeof(DateTime)};
- }
- }
- public override Type[] DeserializableTypes
- {
- get
- {
- return new Type[]{typeof(DateTime)};
- }
- }
- }
- }