Objective c 将Obj-C中的24小时时间格式转换为12小时时间格式
我现在以24小时格式显示时间,因为这是我现在用现有数据做的最简单的事情 我用“午夜后的分钟数”来表示时间,例如,上午7:00或7:00表示“420”,晚上21:30或9:30表示“1290”,依此类推Objective c 将Obj-C中的24小时时间格式转换为12小时时间格式,objective-c,nsdate,nsdateformatter,Objective C,Nsdate,Nsdateformatter,我现在以24小时格式显示时间,因为这是我现在用现有数据做的最简单的事情 我用“午夜后的分钟数”来表示时间,例如,上午7:00或7:00表示“420”,晚上21:30或9:30表示“1290”,依此类推 [NSString stringWithFormat:@"%02d:%02d - %02d:%02d", (open / 60), (open % 60), (close / 60), (close % 60)] 是否有一种很好的方法可以使用NSDateFormatter从24小时转换到12小时
[NSString stringWithFormat:@"%02d:%02d - %02d:%02d", (open / 60), (open % 60), (close / 60), (close % 60)]
是否有一种很好的方法可以使用NSDateFormatter从24小时转换到12小时?我试过很多方法,但我从来没有得到100%正确的格式
我也尝试了很多if语句,结果代码行太多,在我看来,对于这样一个相对“简单”的工作来说,这应该是完全不必要的
此外,无论我怎么做,我也会在开始时没有“1”的情况下,在12小时内出现错误的格式,例如“09:30 a.m.”,等等。我可以通过查找后缀来去除这些内容,但这似乎又是乏味和奇怪的。检查你的分钟数是否少于720(12小时),如果是,如果不是下午(因此,你可以用小时-12从军队到12小时)然后根据需要添加后缀。这并不漂亮,但它是一个相对简单的格式化工作。检查你的分钟数是否少于720(12小时),如果是,是AM,如果不是PM(因此你可以用小时-12从军队到12小时)然后根据需要添加后缀。这并不漂亮,但它是一项相对简单的格式化工作。您应该真正使用系统的默认日期格式:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:hours];
[comps setMinute:minutes];
NSDate* date = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSString* dateString = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterLongStyle];
或者如果你坚持你能做到
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSString* dateString = [dateFormatter stringFromDate:date];
您应该真正使用系统的默认日期格式:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:hours];
[comps setMinute:minutes];
NSDate* date = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSString* dateString = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterLongStyle];
或者如果你坚持你能做到
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSString* dateString = [dateFormatter stringFromDate:date];
对于像我这样的新手,我努力使用日期格式化程序,发现[NSCalendar currentCalendar]将使用用户首选项来设置时区。在我的情况下,我想转换服务器给我的时间,所以它总是错误的。在我的例子中,我使用了这个简单的函数
- (NSString *)formatTime:(NSString *)time
{
NSString *hour = [time substringWithRange:NSMakeRange(0, 2)];
NSString *minute = [time substringWithRange:NSMakeRange(2, 2)];
NSString *tail = ([hour integerValue] > 11) ? @"PM" : @"AM";
return [NSString stringWithFormat:@"%@:%@ %@", hour, minute, tail];
}
对于像我这样的新手,我努力使用日期格式化程序,发现[NSCalendar currentCalendar]将使用用户首选项来设置时区。在我的情况下,我想转换服务器给我的时间,所以它总是错误的。我在我的例子中使用了这个简单的函数
- (NSString *)formatTime:(NSString *)time
{
NSString *hour = [time substringWithRange:NSMakeRange(0, 2)];
NSString *minute = [time substringWithRange:NSMakeRange(2, 2)];
NSString *tail = ([hour integerValue] > 11) ? @"PM" : @"AM";
return [NSString stringWithFormat:@"%@:%@ %@", hour, minute, tail];
}
你可以试试这个。它对我有用
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"yyyy-mm-dd hh:mm:ss"];
NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
[df setDateFormat:@"hh:mm a"];
newDate = [df stringFromDate:[NSDate date]];
你可以试试这个。它对我有用
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"yyyy-mm-dd hh:mm:ss"];
NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
[df setDateFormat:@"hh:mm a"];
newDate = [df stringFromDate:[NSDate date]];
是的,这就是我为“如果语句解决方案”所做的,就像说的;不漂亮:)日期格式通常不是..这不是很多如果..它是一个,也许是两个。是的,这就是我为“如果语句解决方案”所做的,就像说的;不太好看:)日期格式通常不是..而且也不是很多如果..是一个,也许是两个。谢谢!我已经合并了上面两个,如果时间有“0”前缀,我就删除它。谢谢!我已经合并了上面的两个,如果时间有“0”前缀,我就删除它。我给了@“HH:mm aa”,它返回的是下午13:45,我从你的回答中得知,使用小写的HH is格式可以返回下午01:45。谢谢:)我给了@HH:mm aa,它返回下午13:45,我从你的回答中得知,使用小写的HH-is格式可以返回下午1:45。谢谢:)它没有把时间改成AM,PMit也没有把时间改成AM,PM