-3

我正在尝试以我的移动应用程序的 Web 服务可以接受的格式获取 NSDate。该应用程序目前2013-03-26 17:27:55在老一代设备(iPhone 3GS、4 和 iOS 模拟器)上以格式输出,其中 12 小时时间以下午 5:27 的格式显示在状态栏中。这很好,这是我需要它用于服务器的格式。

当使用更新的设备(包括 iPhone 4S 和 iPhone 5)时,我的问题出现了,其中 12 小时时间以下午 5:27 的格式显示在状态栏中。在这些设备上,应用程序以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)

那么,如何在用户的常规设置中关闭 24 小时制时,将 4S 及之后的输出日期设为 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]];
4

1 回答 1

1

使用日期格式化程序

- (NSString *)dateStringForWebService:(NSDate *)date
{
    //If you call this often, you should cache the date formatter object
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss"];
    NSString * formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}
于 2013-03-26T18:36:43.787 回答