AjaxPro automatically change the time zone of DateTime parameters from
client to server and vice versa.

My team is facing a problem here as we do the time zone conversion on
the server instead of on the client side. Therefore, we have to
converse DateTime object into string before passing of data to prevent
double conversion which is very tedious and messy.

Is there any way to disable the auto datetime conversion?

I have read somewhere that you can do a workaround by implementing your
own DateTimeConverter. I tried that but it doesn't seem to work. Is the
below implementation correct?

Here the web.config,

<configSections>
   <sectionGroup name="ajaxNet">
     <section name="ajaxConverters"
type="Ajax.AjaxConverterSectionHandler, Ajax" />
   </sectionGroup>
</configSections>

<ajaxNet>
       <ajaxConverters>
               <remove type="Ajax.JSON.DateTimeConverter , Ajax" />
               <add type="MySample.DateTimeConverter, MySample" />
       </ajaxConverters>
</ajaxNet>


In MySample project

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using AjaxPro;

namespace AjaxPro
{
    public class DateTimeConverter : IJavaScriptConverter
    {
        public DateTimeConverter(): base()
        {
            m_serializableTypes = new Type[] { typeof(DateTime) };
            m_deserializableTypes = new Type[] { typeof(DateTime) };
        }

        public override object Deserialize(IJavaScriptObject o, Type t)
        {
            JavaScriptObject ht = o as JavaScriptObject;

            if (o is JavaScriptSource)
            {
                // new Date(Date.UTC(2006,6,9,5,36,18,875))

                string s = o.ToString();

                if (s.StartsWith("new Date(Date.UTC(") &&
s.EndsWith("))"))
                {
                    s = s.Substring(18, s.Length - 20);

                    // add more checks 0...2000, 0..11, 1..31, 0..23,
0..59, 0..59, 0..999
                    Regex r = new
Regex(@"(\d{4}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,3})");
                    Match m = r.Match(s);

                    if (m.Groups.Count != 8)
                        throw new NotSupportedException();

                    int Year = int.Parse(m.Groups[1].Value);
                    int Month = int.Parse(m.Groups[2].Value) + 1;
                    int Day = int.Parse(m.Groups[3].Value);
                    int Hour = int.Parse(m.Groups[4].Value);
                    int Minute = int.Parse(m.Groups[5].Value);
                    int Second = int.Parse(m.Groups[6].Value);
                    int Millisecond = int.Parse(m.Groups[7].Value);

                    DateTime d = new DateTime(Year, Month, Day, Hour,
Minute, Second, Millisecond);
                    //return
d.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d).TotalMinutes);
                    return d;
                }
                else if (s.StartsWith("new Date(") && s.EndsWith(")"))
                {
                    long nanosecs = long.Parse(s.Substring(9, s.Length
- 10)) * 10000;
                    #if(NET20)
                                        nanosecs += new DateTime(1970,
1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
                                        DateTime d1 = new
DateTime(nanosecs, DateTimeKind.Utc);
                    #else
                                        nanosecs += new DateTime(1970,
1, 1, 0, 0, 0, 0).Ticks;
                                        DateTime d1 = new
DateTime(nanosecs);
                    #endif

                    //return
d1.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d1).TotalMinutes);
                    return d1;
                }
            }

            if (ht == null)
                throw new NotSupportedException();

            int Year2 =
(int)JavaScriptDeserializer.Deserialize(ht["Year"], typeof(int));
            int Month2 =
(int)JavaScriptDeserializer.Deserialize(ht["Month"], typeof(int));
            int Day2 =
(int)JavaScriptDeserializer.Deserialize(ht["Day"], typeof(int));
            int Hour2 =
(int)JavaScriptDeserializer.Deserialize(ht["Hour"], typeof(int));
            int Minute2 =
(int)JavaScriptDeserializer.Deserialize(ht["Minute"], typeof(int));
            int Second2 =
(int)JavaScriptDeserializer.Deserialize(ht["Second"], typeof(int));
            int Millisecond2 =
(int)JavaScriptDeserializer.Deserialize(ht["Millisecond"],
typeof(int));

            DateTime d2 = new DateTime(Year2, Month2, Day2, Hour2,
Minute2, Second2, Millisecond2);
            //return
d2.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d2).TotalMinutes);
            return d2;
        }

        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);
        } 
    }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Ajax.NET Professional" group.

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]

For more options, visit this group at http://groups.google.com/group/ajaxpro

The latest downloads of Ajax.NET Professional can be found at 
http://www.ajaxpro.info/

Don't forget to read my blog at http://weblogs.asp.net/mschwarz/
-~----------~----~----~----~------~----~------~--~---

Reply via email to