我有这个代码应该计算一个月的开始和结束日期(日期在NSPredicate中用于在CoreData商店中搜索月初和月末之间的记录)
NSDate *startOfMonth;
NSDate *endOfMonth;
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSTimeInterval interval;
[cal rangeOfUnit:NSMonthCalendarUnit startDate:&startOfMonth interval:&interval forDate:now];
endOfMonth = [startOfMonth dateByAddingTimeInterval:interval-1];
NSLog(@"\n\nstartOfMonth: %@\nendOfMonth: %@\ninterval: %f",startOfMonth, endOfMonth, interval);
这是NSLog的输出:
startOfMonth: 2014-11-01 07:00:00 +0000
endOfMonth: 2014-12-01 07:59:59 +0000
interval: 2595600.000000
如您所见, endOfMonth 不正确;它应该是2014-11-30 ......代码有什么问题? (这不是我的,我在SO上找到了它)
我更改了“cal”的timeZone属性,但仍然得到了相同的日志消息。
答案 0 :(得分:1)
startOfMonth
也打印不正确。
时间实际上是正确的,但是当你打印时,它会打印你的时区。
打印日期将在UTC时区中打印该日期。您可以在设定的时区打印,如下所示:
NSLog(@"\n\nstartOfMonth: %@\nendOfMonth: %@\ninterval: %d",[startOfMonth descriptionWithLocale:[NSLocale systemLocale]], [endOfMonth descriptionWithLocale:[NSLocale systemLocale]], (int)interval);
要更新日期,请尝试以下操作:
[cal rangeOfUnit:NSMonthCalendarUnit startDate:&startOfMonth interval:&interval forDate:now];
NSTimeInterval timeZoneOffset = [[NSTimeZone localTimeZone] secondsFromGMT];
startOfMonth = [startOfMonth dateByAddingTimeInterval:timeZoneOffset];
endOfMonth = [startOfMonth dateByAddingTimeInterval:interval-1];
NSLog(@"\n\nstartOfMonth: %@\nendOfMonth: %@\ninterval: %d",startOfMonth, endOfMonth, (int)interval);
更改日历也有效:
cal.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
[cal rangeOfUnit:NSMonthCalendarUnit startDate:&startOfMonth interval:&interval forDate:now];
答案 1 :(得分:1)
您需要做的是将UTC转换为感兴趣的时区。
您希望将日历的timeZone属性设置为您要使用的时区。以下代码来自the Apple documentation,将时区设置为中央夏令时:
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CDT"]];
答案 2 :(得分:0)
在这里,我可以发布你想要的代码..
-(NSMutableDictionary *)getMonthsStartAndEndDatesFromDate:(NSDate *)date
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:( NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:date];
components.day = 1;
NSDate *startDate = [[gregorian dateFromComponents:components] dateByAddingTimeInterval:0];
NSDateComponents *components1=[gregorian components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date];
//NSEraCalendarUnit |
NSInteger month=[components1 month];
NSInteger year=[components1 year];
if (month==12)
{
[components1 setYear:year+1];
[components1 setMonth:1];
}
else {
[components1 setMonth:month+1];
}
[components1 setDay:0];
NSDate *endDate =[[gregorian dateFromComponents:components1] dateByAddingTimeInterval:0];
NSMutableDictionary *dictTemp = [[NSMutableDictionary alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
startDate = [self dateFromDate:startDate WithHour:0 minute:0 second:01];
endDate =[self dateFromDate:endDate WithHour:23 minute:59 second:59];
NSString *StrHour = [dateFormatter stringFromDate:startDate];
NSString *strMin = [dateFormatter stringFromDate:endDate];
[dictTemp setObject:StrHour forKey:@"StartDate"];
[dictTemp setObject:strMin forKey:@"EndDate"];
return dictTemp;
}
-(NSDate *) dateFromDate:(NSDate *)date
WithHour:(NSInteger)hour
minute:(NSInteger)minute
second:(NSInteger)second
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: NSYearCalendarUnit|
NSMonthCalendarUnit|
NSDayCalendarUnit
fromDate:date];
[components setHour:hour];
[components setMinute:minute];
[components setSecond:second];
NSDate *newDate = [calendar dateFromComponents:components];
return newDate;
}
[self getMonthsStartAndEndDatesFromDate:[NSDate new]];
此方法返回开始和结束日期 像这样
EndDate =“2014-11-30 23:59:59 GMT + 5:30”; StartDate =“2014-11-01 00:01:00 GMT + 5:30”;
所以这可能对你有所帮助..