我试图让我的移动应用程序的Web服务可以接受的格式的NSDate。该应用程序目前正在使用老一代设备(iPhone 3GS,4和iOS模拟器)上的2013-03-26 17:27:55
格式输出,其中12小时时间以格式为5:27 PM的格式显示在状态栏中。这很好,而且这是我需要用于服务器的格式。如何根据NSLocale强制NSDate对象在24小时时间关闭时遵循特定格式?
当使用较新的设备(包括iPhone 4S和iPhone 5),12小时时间以格式5:27 p.m在状态栏中出现时,出现问题。在这些设备上,应用以2013-03-26 05:27:55 PM
的格式输出。这里的问题的总结:
Web service accepts yyyy-mm-dd HH:MM:ss
iPhone 4 and earlier yyyy-mm-dd HH:MM:ss (correct)
iPhone 4s and later yyyy-mm-dd HH:MM:ss AM/PM (incorrect and rejected)
所以,我怎样才能在4S输出的日期,后来是在24小时格式时,24小时的时间是关在用户的常规设置?
编辑:
我用下面的方法(NSDate *)getCurrentDate
尝试和修改的NSDate对象的格式。
NSDate *currentDateAndTime = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"AM Symbol: %@", formatter.AMSymbol);
NSLog(@"PM Symbol: %@", formatter.PMSymbol);
NSString *dateString = [formatter stringFromDate:currentDateAndTime];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"currentDate: %@ currentDateString: %@", date, dateString);
return [formatter dateFromString:[formatter stringFromDate:currentDateAndTime]];
你是如何在当前的应用程序转换的NSDate到的NSString? – Anupdas 2013-03-26 17:47:57
您是否尝试过使用NSDateFormatter? – 2013-03-26 17:51:51
对于这样的特定格式,您确实需要使用带有指定格式字符串的NSDateFormatter。对于人类消费来说,这是令人不悦的,更喜欢默认设置。对于机器消耗来说,这是非常必要的。 – gaige 2013-03-26 17:52:49