tags:

views:

1618

answers:

3

Hi,

I am experiencing a very strange behavoiour of Flex Date object. My web service is written in .Net 3.5 and all object which I am retriving or updating have Creation Date (Date Type) in .Net code.

But when I am calling .Net web service and displaying data in Flex, Flex displaying a different Date than what stored in web service. When I update my object using Flex UI, every time update time is very different than actual update time set by .Net code.

Can any one help me solve this ?

A: 

I don't have an immediate answer, but some of the factors to consider are:

  1. Is the date ambiguous? You didn't supply a sample of what the return might be, but a date like 09/10/2009 is ambiguous unless both ends agree on the format (dd/mm/yyyy or mm/dd/yyy).

  2. Flex is basically a form of ECMAScript and a format from IETF RFC 1123 Section 5.2.14 should parse correctly. For example:

    Mon, 28 Sep 2009 21:22:00 GMT

The Date object in .NET should be able to produce that (I can't remember off the top of my head) and the Date object in Flex should be able to parse it.

John Cavan
+3  A: 

Your date issues may be a result of how timezones and serialization are handled between Flex and your server. I've had problems with Flex dates and Java so I will explain somethings encountered there:

  • This describes how Flex transfers dates as UTC without timezone information.
  • You should also understand the assumptions .Net makes about timezones and daylight savings times for dates. I believe Java assumed the dates in the database were in the timezone of the server.
  • To not confuse myself while debugging I found comparing the UTC on the Flex and Server side useful.
  • You can simulate other timezones on your local machine for Flex by simply changing the computers time zone. I don't know if this works for .Net.
  • In the end, I used custom serialization to transfer dates as strings because constructing dates from strings was easier for me than understanding the implicit serialization that was happening.
Brandon
A: 

Flex app.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
   creationComplete="init();">
<mx:Script>
 <![CDATA[
  import mx.rpc.events.ResultEvent;

  private function init():void
  {
   updateFlexTime();
  }

  private function updateFlexTime():void
  {
   flexTimeLabel.text = new Date().toString();
   flexLocalTimeLabel.text = new Date().toLocaleString();
  }

  private function refreshHandler(event:Event):void
  {
   dateTimeService.GetCurrentDateTimeAsString();
  }

  private function onGetCurrentDateTimeAsString(event:ResultEvent):void
  {
   var value:String = String(event.result);
   var currentDate:Date = new Date(Date.parse(value));

   remoteTimeLabel.text = currentDate.toString();
   remoteLocalTimeLabel.text = currentDate.toLocaleString();

   updateFlexTime();
  }
 ]]>
</mx:Script>
<mx:Form>
 <mx:FormItem label="Flex time:">
  <mx:Text id="flexTimeLabel"/>
 </mx:FormItem>

 <mx:FormItem label="Remote time:">
  <mx:Text id="remoteTimeLabel"
     text="Press refresh button."/>
 </mx:FormItem>

 <mx:FormItem label="Flex local time:">
  <mx:Text id="flexLocalTimeLabel"/>
 </mx:FormItem>

 <mx:FormItem label="Remote local time:">
  <mx:Text id="remoteLocalTimeLabel"
     text="Press refresh button."/>
 </mx:FormItem>

 <mx:Button label="Refresh"
      click="refreshHandler(event)"/>
</mx:Form>
<mx:WebService id="dateTimeService"
      endpointURI="http://localhost:2054/TestService.asmx"
      wsdl="http://localhost:2054/TestService.asmx?wsdl"&gt;
 <mx:operation name="GetCurrentDateTimeAsString"
      resultFormat="object"
      result="onGetCurrentDateTimeAsString(event)"/>
</mx:WebService>

.Net WebService:

namespace Lab.StackOverflow
{
    using System;
    using System.Web.Services;

    /// <summary>
    /// Test date/time service
    /// </summary>
    [WebService(Namespace = "http://lab.stackoverflow.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class TestService : WebService
    {
        /// <summary>
        /// Get current date and time
        /// </summary>
        /// <returns>
        /// Return UTC date time by RFC 1123 standard. 
        /// </returns>
        [WebMethod]
        public string GetCurrentDateTimeAsString()
        {
            return string.Format("{0:r}{1:zz}", DateTime.Now, DateTime.Now);
        }
    }
}
Dmitriy Sosunov
Thanks for your example.Web service is used by few other app and web service team can not change definition. Is there any method to convert Datetime returned by web service to correct date format. Assuming web service will return datetiem NOT String.I tried to convert result returned from .Net web service to XML.Web service is sending "2009-09-30T15:44:36.4667877-06:00" and flex is getting 2009-09-30T23:02:23.877Z in XML.It has no consistant logic. Everytime I update object, modified time is very different from old value. I can't understand what logic Flex using to convert Datetime value.
Chandra P Singh