Number of day of current year in iOS

后端 未结 4 555
清歌不尽
清歌不尽 2021-01-16 18:30

I want to find number of day today is in current year. e.g, if today is Mar15, 2012 I should get 75(31 + 29 + 15). Or we can simply say that number of days between today and

4条回答
  •  难免孤独
    2021-01-16 19:08

    Using NSDateComponents you can can gather the NSDayCalendarUnit component which should indicate the current day of the year.

    Something along the lines of the following should suit your needs:

    //create calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    //set calendar time zone
    [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    
    //gather date components
    NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:[NSDate date]];
    
    //gather time components
    NSInteger day = [components day];
    

提交回复
热议问题