我需要从NSDate中移除时间组件,例如:我的值为'2012-08-12 22:15:54 +00000',但我得到了值'2012-08-12 03:00:00 00000'
我正在使用的代码是:
NSDate* getNDateOfWeek(int week, int day) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.timeZone = [NSTimeZone systemTimeZone];
unsigned unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:now];
[comp setWeekday:day];
[comp setWeek: week];
[comp setHour:0];
NSDate *resultDate = [calendar dateFromComponents:comp];
return resultDate;
}
任何帮助?
答案 0 :(得分:0)
你可能试图获得不同的结果,但如果你只是在寻找'今天'的日期那么类似的东西:
// Output -> 'Mon, Jan 16, 2012'
- (NSString *)todaysDate
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE MMM d Y"];
return [dateFormat stringFromDate:[[NSDate alloc] init]];
}