Search code examples
c#iosjsonnsdatensdateformatter

Send datetime from iPhone app to a webservice


From iPhone application, I have to send date as a parameter to a webservice method where server parsing logic is implemented with C#, .NET and JSON.

In my iPhone application I am formatting the date as:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSString *myDateString = [dateFormatter stringFromDate:SentOn];

I am getting the error:

There was an error deserializing the object of type DashboardDataContract.Contracts.DashboardInputParams. DateTime content '2013-04-04T12:00:00Z' does not start with '/Date(' and end with ')/' as required for JSON.'.

I tried different formatting of dates.


Solution

  • By default the JSON.Net library will emit all dates using the ISO Date format e.g "2012-05-09T00:00:00Z", but Microsoft’s JSON formatter for date will emit using Microsoft’s own format “/Date(xxxxxxxxxxxx)/”

    You can try this:

    unsigned long long time=(([[NSDate date] timeIntervalSince1970])*1000);
    NSString* dateTimeTosend= [NSString stringWithFormat:@"/Date(%lld)/",time];