2 garaster garaster 于 2012.12.21 14:44 提问

IOS中获取当日上午10点

如何获取当日上午10点和第二天上午10点?

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

这个响应的不对。不知道怎么实现?

1个回答

redCoral_
redCoral_   2012.12.21 15:59
已采纳

试试这代码,如果要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];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!