如何在iphone中以12小时格式显示时间

如何在iphone中以12小时格式显示时间,iphone,objective-c,xcode4,Iphone,Objective C,Xcode4,我正在开发一个应用程序,每当用户添加一些细节时,记录就会以24小时格式存储在数据库中。但出于显示目的,我以12小时的格式显示时间。i、 我使用了“dd-MMM-yyyy-hh:mm-a”格式来显示时间,并且它在我的屏幕上正确显示,但问题是每当用户在设置中将日期/时间格式从12小时更改为24小时格式时。我的应用程序开始以24小时格式显示时间。即使用户在设置中将设置更改为24小时格式,是否有人可以帮助我。时间格式不应从12小时更改为24小时 一般来说,您应该允许用户选择如何显示它 使用您的代码,您应

我正在开发一个应用程序,每当用户添加一些细节时,记录就会以24小时格式存储在数据库中。但出于显示目的,我以12小时的格式显示时间。i、 我使用了“dd-MMM-yyyy-hh:mm-a”格式来显示时间,并且它在我的屏幕上正确显示,但问题是每当用户在设置中将日期/时间格式从12小时更改为24小时格式时。我的应用程序开始以24小时格式显示时间。即使用户在设置中将设置更改为24小时格式,是否有人可以帮助我。时间格式不应从12小时更改为24小时

一般来说,您应该允许用户选择如何显示它

使用您的代码,您应该能够执行以下操作:

dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLocale *twelveHourLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
dateFormat.locale = twelveHourLocale;
CreatedDate = [dateFormat dateFromString:[NSString stringWithFormat:@"%@",[serviceAntInstance.arr_CreatedDate objectAtIndex:indexPath.row]]];
[dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm a"];
createdDateConvert = [dateFormat stringFromDate:CreatedDate];

通常,您应该允许用户选择如何显示它

使用您的代码,您应该能够执行以下操作:

dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLocale *twelveHourLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
dateFormat.locale = twelveHourLocale;
CreatedDate = [dateFormat dateFromString:[NSString stringWithFormat:@"%@",[serviceAntInstance.arr_CreatedDate objectAtIndex:indexPath.row]]];
[dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm a"];
createdDateConvert = [dateFormat stringFromDate:CreatedDate];

请查看以下内容:

NSDateFormatter *formatterTemp = [[NSDateFormatter alloc] init];
[formatterTemp setLocale:[NSLocale currentLocale]];
[formatterTemp setDateStyle:NSDateFormatterNoStyle];
[formatterTemp setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];

NSRange amRange = [dateString rangeOfString:[formatterTemp AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatterTemp PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatterTemp release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

请查看以下内容:

NSDateFormatter *formatterTemp = [[NSDateFormatter alloc] init];
[formatterTemp setLocale:[NSLocale currentLocale]];
[formatterTemp setDateStyle:NSDateFormatterNoStyle];
[formatterTemp setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];

NSRange amRange = [dateString rangeOfString:[formatterTemp AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatterTemp PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatterTemp release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

如何利用现有的区域设置

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setLocale: locale];
[formatter setTimeStyle: NSDateFormatterShortStyle];

NSString *result = [formatter stringFromDate: [NSDate date]];

[formatter release];
[locale release];

如何利用现有的区域设置

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setLocale: locale];
[formatter setTimeStyle: NSDateFormatterShortStyle];

NSString *result = [formatter stringFromDate: [NSDate date]];

[formatter release];
[locale release];

下面是我是否将datetime格式转换为另一种格式的代码*/dateFormat=[[[NSDateFormatter alloc]init]autorelease];[日期格式setDateFormat:@“yyyy-MM-dd HH:MM:ss”];CreatedDate=[dateFormat dateFromString:[NSString stringWithFormat:@“%@,[serviceAntInstance.arr\u CreatedDate objectAtIndex:indexPath.row]];[dateFormat setDateFormat:@“dd-MMM-yyyy hh:mm-a”];CreatedDataConvert=[dateFormat stringFromDate:CreatedDate];为用户区域设置指定了时间格式。这是首选设置,您的应用程序应该以这样一种方式显示日期信息,用户必须能够方便地感知它。因此,一般来说,您不应该将格式更改为12-h。我编辑了答案,以显示如何强制使用12小时格式的区域设置。这是来自苹果公司的文档:下面是我是否转换datetime格式/*以将日期转换为另一种格式的代码*/dateFormat=[[NSDateFormatter alloc]init]autorelease];[日期格式setDateFormat:@“yyyy-MM-dd HH:MM:ss”];CreatedDate=[dateFormat dateFromString:[NSString stringWithFormat:@“%@,[serviceAntInstance.arr\u CreatedDate objectAtIndex:indexPath.row]];[dateFormat setDateFormat:@“dd-MMM-yyyy hh:mm-a”];CreatedDataConvert=[dateFormat stringFromDate:CreatedDate];为用户区域设置指定了时间格式。这是首选设置,您的应用程序应该以这样一种方式显示日期信息,用户必须能够方便地感知它。因此,一般来说,您不应该将格式更改为12-h。我编辑了答案,以显示如何强制使用12小时格式的区域设置。这来自苹果的文档:如果用户将时间格式从12小时更改为24小时,那是因为他/她喜欢这样。你应该尊重用户的选择。如果用户将时间格式从12小时更改为24小时,那是因为他/她喜欢这样。你应该尊重用户的选择。