从iphone应用程序发送日期时间到web服务
问题描述:
从iPhone应用程序,我必须发送日期作为参数到一个webservice方法,其中服务器解析逻辑用C#,.NET和JSON实现。从iphone应用程序发送日期时间到web服务
在我的iPhone应用程序,我格式化日期:
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];
我收到错误:
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.'.
我试图日期的不同格式。在这方面有人能帮助我吗?
答
默认情况下,JSON.Net库会发出使用ISO日期格式如"2012-05-09T00:00:00Z"
所有日期,但微软的JSON格式的日期会发出使用微软自己的格式“/Date(xxxxxxxxxxxx)/”
你可以试试这个:
unsigned long long time=(([[NSDate date] timeIntervalSince1970])*1000);
NSString* dateTimeTosend= [NSString stringWithFormat:@"/Date(%lld)/",time];
答
将NSDate发送到服务器的最有效方法是获取timestamp。你基本上可以用
NSTimeInterval timestamp = [[NSDate date] timeInvervalSince1970];
答
得到它可以在NSDate
- (NSString *)jsonDateString
{
NSTimeInterval seconds = [self timeIntervalSince1970];
return [NSString stringWithFormat:@"/Date(%.0f+0000)/",seconds*1000];
}
非常好,简单的创建一个类的方法! – user2068793 2013-04-04 11:54:08
这是否可以在所有时区使用? – Anupdas 2013-04-04 12:03:52
如果您想添加时区,您需要格式为/ Date(xxxxxxxxxxxx + yyyy)/其中yyyy是时区,例如:/ Date(12345678954 + 0000)/ – 2013-04-04 12:06:35