Iphone NSDate专家:我的计算方法无法生成一致的日期。为什么?
我创建了一个方法来查找每个月的第三个星期五 当我在东部时区运行它时,一切都很好 当我从中央运行它时,日期是不确定的 我的方法:Iphone NSDate专家:我的计算方法无法生成一致的日期。为什么?,iphone,objective-c,Iphone,Objective C,我创建了一个方法来查找每个月的第三个星期五 当我在东部时区运行它时,一切都很好 当我从中央运行它时,日期是不确定的 我的方法: -(NSDate*) getSaturdayAfterThirdFriday:(NSDate*)date { NSDate* resultDate = nil; if( date != nil ) { NSDateFormatter *df = [[NSDateFormatter alloc] init]; // s
-(NSDate*) getSaturdayAfterThirdFriday:(NSDate*)date {
NSDate* resultDate = nil;
if( date != nil ) {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// start at the 1st of the month
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"EST"];
[calendar setTimeZone:zone];
NSDateComponents *comp = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
[comp setDay:1];
date= [calendar dateFromComponents:comp];
[calendar release];
// count number of 'fri' (including today)
BOOL foundFlag = NO;
NSString* dateString = nil;
NSInteger count = 0;
[df setDateFormat:@"EEE"];
while (foundFlag == NO) {
dateString = [df stringFromDate:date];
if( [dateString caseInsensitiveCompare:@"fri"] == 0 ) {
count++;
NSLog(@"Found Friday: %@", [date description]);
}
if( count >= 3 ) foundFlag = YES;
else date = [NSDate dateWithTimeInterval:86400 sinceDate:date];
}
// get the next day (Saturday)
resultDate = [NSDate dateWithTimeInterval:86400 sinceDate:date];
[df release];
}
return resultDate;
}
通过以下方法创建传递的日期:
-(NSDate*) getDateForEasternTime:(NSDate*)date {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"EST"];
[calendar setTimeZone:zone];
NSDateComponents *comp = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
date = [calendar dateFromComponents:comp];
[calendar release];
NSLog(@"getDateForEasternTime: %@", [date description]);
return date;
}
我只需要根据东部时间计算日期
以下是我在东部时区的日志输出:
getDateForEasternTime: 2010-11-14 05:00:00 GMT
Found Friday: 2010-11-05 05:00:00 GMT
Found Friday: 2010-11-12 05:00:00 GMT
Found Friday: 2010-11-19 05:00:00 GMT
中央时间:
getDateForEasternTime: 2010-11-14 05:00:00 GMT
Found Friday: 2010-11-05 05:00:00 GMT
Found Friday: 2010-11-13 05:00:00 GMT
Found Friday: 2010-11-20 05:00:00 GMT
更奇怪的是,如果我在时区上再往西走,就会出现差异。如果我去东部时区,那里就没有了
任何NSDate大师都能帮上忙吗
编辑:
当我提到改变时区时,我指的是改变设备本身的时区。该行:
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"EST"];
保持不变。所有代码都是不必要的。使用
NSDateComponents
的weekday
和weekdayOrdinal
属性,您可以直接查询一个月的第三个星期五的NSCalendar
:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:6]; // 1 = Sunday ... 7 = Saturday
[components setWeekdayOrdinal:3]; // 3rd Friday
[components setMonth:11];
[components setYear:2010];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *date = [currentCalendar dateFromComponents:components];
此外,在中也有类似的例子。所有代码都是不必要的。使用
NSDateComponents
的weekday
和weekdayOrdinal
属性,您可以直接查询一个月的第三个星期五的NSCalendar
:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:6]; // 1 = Sunday ... 7 = Saturday
[components setWeekdayOrdinal:3]; // 3rd Friday
[components setMonth:11];
[components setYear:2010];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *date = [currentCalendar dateFromComponents:components];
另外,在.中也有类似的例子,非常好,但是我如何保证日期是在东部时间?使用NSTimeZone*zone=[NSTimeZone timeZoneWithName:@“EST”];[日历设置时区:时区];然后将其作为日历而不是[NSCalendar currentCalendar]传递?无论设备时区如何,它都能在东部工作吗?很好,但我如何保证日期是东部时间?使用NSTimeZone*zone=[NSTimeZone timeZoneWithName:@“EST”];[日历设置时区:时区];然后将其作为日历而不是[NSCalendar currentCalendar]传递?无论设备时区如何,它都能在东部工作吗?