I am working on application that required iPhone date and time to post开发者_如何学JAVA on server. now this date and time on different devices are always different all over the world based on region and time zone. I would like to have local date time in 24 hrs only, though if the user has changed the setting>date & time>24 hour time>off
As I am not getting the time in 'yyyy-MM-dd HH:mm:ss' format, it is creating issue on sever side and showing wrong date and time on server. Is there any very specific solution for this?
I'd suggest you should avoid sending formatted dates to a server and send UNIX timestamps instead. [[NSDate date] timeIntervalSince1970]
gives you a time zone-independent timestamp which you don't have to parse. If you need the time zone too, send its identifier as an additional parameter.
If you need time in 24 hour format then use capital H in dateFormatter:
dateFormatter.dateFormat = @"yyyy MM dd **HH**:mm:ss";
Like Costique says, use an UNIX timestamp, but if you must have it in a String format, include the time zone in the format string and handle it on the server, you need something like:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc]
initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ssZZ"];
NSString *transDateStr = [dateFormatter stringFromDate:transDate];
[dateFormatter release];
精彩评论