如何獲取當日上午10點和第二天上午10點?
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];[nowComponents setHour:10];
這個響應的不對。不知道怎麼實現?
試試這代碼,如果要10 am
,設置小時為34
NSDate *date = [NSDate date];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
[gregorian setTimeZone:gmt];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 10];
[components setMinute:0];
[components setSecond: 0];
NSDate *newDate = [gregorian dateFromComponents: components];
NSLog(@"Date%@", newDate);
[gregorian release];