Bishan
Bishan

Reputation: 15702

Issue with asmx webservice Date object

I'm parsing Date object to web service using java script as below.

HRA_Create.HelloWorld(new Date("2013-07-08 00:00:00"));

but when i check this Date in asmx WebMethod, display as 7/7/2013 6:00:00 PM.

I debug my app and see the value as below.

Debug.WriteLine(dt.ToString());

What could be the error ? how can i solve this ?

    [WebMethod]
    public string HelloWorld(DateTime dt)
    {
        Debug.WriteLine(dt.ToString());

        return dt.ToString();
    }

Java Script

<script type="text/javascript">
    function callServer() {
        HRA_Create.HelloWorld(new Date("2013-07-08 00:00:00"));
    }
</script>

ScriptManager

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/HRA_Create.asmx" />
    </Services>
</asp:ScriptManager>

Java Script Call

<a href="javascript:callServer()">Call Server</a>

Upvotes: 0

Views: 536

Answers (1)

Alex Filipovici
Alex Filipovici

Reputation: 32541

You need to use one of the following ISO 8601 compliant W3C formats:

Year:
   YYYY (eg 1997)
Year and month:
   YYYY-MM (eg 1997-07)
Complete date:
   YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
   YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
   YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
   YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

where:

YYYY = four-digit year
MM   = two-digit month (01=January, etc.)
DD   = two-digit day of month (01 through 31)
hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
mm   = two digits of minute (00 through 59)
ss   = two digits of second (00 through 59)
s    = one or more digits representing a decimal fraction of a second
TZD  = time zone designator (Z or +hh:mm or -hh:mm)

In your example, any of the following should send the correct date:

HRA_Create.HelloWorld(new Date("2013-07-08T00:00:00Z"));
HRA_Create.HelloWorld(new Date("2013-07-08"))

Upvotes: 1

Related Questions