【问题标题】:Not removing the Time Components from NSDate不从 NSDate 中删除时间组件
【发布时间】:2012-08-12 22:23:40
【问题描述】:

例如,我需要从 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;

}

有什么帮助吗?

【问题讨论】:

    标签: objective-c nsdate nsdatecomponents


    【解决方案1】:

    您可能正在尝试获得不同的结果,但如果您只是在寻找“今天”日期,那么类似以下内容呢:

    // Output -> 'Mon, Jan 16, 2012'
    - (NSString *)todaysDate
    {
       NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
       [dateFormat setDateFormat:@"EEE MMM d Y"];    
       return [dateFormat stringFromDate:[[NSDate alloc] init]];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      相关资源
      最近更新 更多