如何在iOS中获取当年的开始日期

时间:2015-05-13 08:20:06

标签: ios objective-c nsdate nscalendar

我不知道在这种情况下如何使用NSDateNSCalendar 帮我解决这个问题。

5 个答案:

答案 0 :(得分:5)

这可以比其他答案更清晰:

Content

DataContext现在在今年1月1日午夜举行。

答案 1 :(得分:3)

restit

答案 2 :(得分:3)

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject!) {
    if (segue?.identifier == "home_to_search") {

       var searchKeyword = txtSearchBarHome.text
       var svc = segue?.destinationViewController as! SearchViewController;
       svc.toPassSearchKeyword = searchKeyword;

       //let nav = segue?.destinationViewController as! UINavigationController
       //let svc = nav.topViewController as! SearchViewController
       //svc.toPassSearchKeyword = searchKeyword;
    }
}

答案 3 :(得分:2)

NSDate *now = [NSDate date];
NSDateComponents *components= [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:now];
components.month = 1;
components.day = 1;
NSDate *firstDayOfYear = [[NSCalendar currentCalendar] dateFromComponents:components];

答案 4 :(得分:0)

 - (NSDate *)firstDateOfCurrentYear
 {

     NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
     [fmt setDateFormat:@"YYYY-01-01 00:00:00 0000"];
     NSString *dateStr = [fmt stringFromDate:[NSDate date]];
     [fmt setDateFormat: @"YYYY-MM-DD HH:mm:ss zzzz"];
     NSDate *firstDate = [fmt dateFromString:dateStr];
     return firstDate;
 }