iOS6 NSDateFormatter默认年份 [英] iOS6 NSDateFormatter default year
问题描述
有没有人注意到iOS6 NSDateFormatter默认为2000年没有年份,而
iOS6:
Has anyone noticed that iOS6 NSDateFormatter defaults to year 2000 when no year is given while in
iOS6:
[[ NSDateFormatter alloc] init] dateFromString:@7:30 am]
[[NSDateFormatter alloc] init] dateFromString: @"7:30am"]
=> 2000年1月1日上午7:30
=> 2000 Jan 1st, 7:30am
iOS5:
=> 1970年1月1日上午7:30
=> 1970 Jan 1st, 7:30am
问题:
1.有更好的方法来比较两个不同的时间吗?我有这个地铁应用程序 PATH Schedule / Map 。地铁时间在数据库中硬编码为numSecondsSince1970。我通过比较出发时间和当前的numSecondsSince1970来给下一班火车到达。
现在我只是将1970年的时间字符串添加到时间字符串2:30 am=>1970 2:30 am但似乎有更好的方式
Question: 1. Is there a better way to compare two different times? I have this subway app PATH Schedule/Map. The subway times are hardcoded in the database as numSecondsSince1970. I give people the next train arriving by comparing departure times with the current numSecondsSince1970. Right now I am just appending the year 1970 to the time string "2:30am" => "1970 2:30am" but it seems like there is a better way
谢谢!
推荐答案
是的我刚刚注意到这个问题。显然这个错误在Apple上报告,请参阅 http://openradar.appspot.com/12358210
Yes I just noticed this issue. Apparently this bug is reported at Apple, see http://openradar.appspot.com/12358210
编辑:这就是我正在处理的项目中处理此问题的方法。 。
this is how I dealt with this issue in a project I'm working on...
// I use the following code in a category to parsing date strings
- (NSDate *)dateWithPath:(NSString *)path format:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
formatter.dateFormat = format;
NSString *string = [self valueWithPath:path];
return [formatter dateFromString:string];
}
// old parsing code that worked fine in iOS 5, but has issues in iOS 6
NSDate *date = [element dateWithPath:@"datum_US" format:@"yyyy-MM-dd"];
NSDate *timeStart = [element dateWithPath:@"aanvang" format:@"HH:mm:ss"];
NSTimeInterval interval = [timeStart timeIntervalSince1970];
match.timeStart = [date dateByAddingTimeInterval:interval];
和修复...
// the following code works fine in both iOS 5 and iOS 6
NSDate *date = [element dateWithPath:@"datum_US" format:@"yyyy-MM-dd"];
NSDate *timeStart = [element dateWithPath:@"aanvang" format:@"HH:mm:ss"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *timeComps = [calendar components:units fromDate:timeStart];
match.timeStart = [calendar dateByAddingComponents:timeComps toDate:date options:0];
这篇关于iOS6 NSDateFormatter默认年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!