ios - 今天上午10点到达

时间:2012-12-18 11:34:07

标签: iphone ios nsdate

今天上午10点和明天上午10点在ios?

NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

[nowComponents setHour:10];

这没有给出适当的回应。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

试试这段代码,希望它能运作。如果您希望明天获得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];