Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper DateTime Format for a Web Service

I have a webservice with a method which is called via a xmlhttprequest object in my javascript. The method accepts a datetime parameter which is subsequently converted to a string and run against the database to perform a calculation.

I get the value from m_txtDateAdd and send off the xmlHttprequest

<asp:textbox id=m_txtDateAdd tabIndex=4 runat="server" Width="96px" Text="<%# Today %>">
</asp:textbox>

which has a validator attacted to it

<asp:CustomValidator id="m_DateAddValidator" runat="server" ErrorMessage="Please Enter a Valid Date" ControlToValidate="m_txtDateAdd">&#x25CF;</asp:CustomValidator>

My webmethod looks something like this

[WebMethod]
public decimal GetTotalCost(DateTime transactionDate)
{
    String sqlDateString = transactionDate.Year+"/"+transactionDate.Month+"/"+transactionDate.Day;

I use sqlDateString as part of the commandtext i send off to the database. Its a legacy application and its inline sql so I don't have the freedom to set up a stored procedure and create and assign parameters in my code behind. This works 90% of the time. The webservice is called on the onchange event of m_txtDateAdd. Every now and again the response i get from the server is

System.ArgumentException: Cannot convert 25/06/2009 to System.DateTime. System.ArgumentException: Cannot convert 25/06/2009 to System.DateTime.

Parameter name: type ---> System.FormatException: String was not recognized as a valid DateTime.

   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.DateTime.Parse(String s, IFormatProvider provider)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
like image 393
user48408 Avatar asked Jul 10 '09 14:07

user48408


People also ask

What is the best DateTime format?

The ISO 8601 notation is today the commonly recommended format of representing date and time as human-readable strings in new plain-text communication protocols and file formats.

What is the DateTime format in API?

It is important to have the proper format for the date field in all API requests. We use basic ISO 8601 format for mixed date-time: YYYY-MM-DDThh:mm:ss. YYYY is the 4-digit year. MM is the 2-digit month (zero-padded)

How do you write time in API?

The pattern for this date and time format is YYYY-MM-DDThh:mm:ss. sTZD . It is recommended to use the ISO-8601 format for representing the date and time in your RESTful web APIs.

How do you format a DateTime?

dd/MM/yyyy — Example: 23/06/2013. yyyy/M/d — Example: 2013/6/23. yyyy-MM-dd — Example: 2013-06-23.


2 Answers

You need to send a DateTime in the correct format for XML: 2009-07-10T12:40Z. From http://en.wikipedia.org/wiki/ISO_8601.


Also, why are you using HttpRequest? Why not just use Add Service Reference?

like image 147
John Saunders Avatar answered Oct 12 '22 02:10

John Saunders


2001-10-26T19:32:52Z

Use such format.

like image 25
Atish Narlawar Avatar answered Oct 12 '22 02:10

Atish Narlawar