Guest User

Untitled

a guest
Jan 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. NSCalendar *currentCalendar = [NSCalendar currentCalendar];
  2. NSDate *today = [NSDate date];
  3. NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
  4. inUnit:NSYearCalendarUnit
  5. forDate:today];
  6.  
  7. //create calendar
  8. NSCalendar *calendar = [NSCalendar currentCalendar];
  9.  
  10. //set calendar time zone
  11. [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  12.  
  13. //gather date components
  14. NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:[NSDate date]];
  15.  
  16. //gather time components
  17. NSInteger day = [components day];
  18.  
  19. NSCalendar *cal = [NSCalendar currentCalendar];
  20. NSDateFormatter *df = [[NSDateFormatter alloc] init];
  21.  
  22. [df setCalendar:cal];
  23. [df setDateFormat:@"DDD"]; // D specifier used for day of year
  24. NSString *dayOfYearString = [df stringFromDate:someDate]; // you choose 'someDate'
  25.  
  26. NSLog(@"The day is: %@", dayOfYearString);
  27.  
  28. // create your NSDate and NSCalendar objects
  29. NSDate *today = [NSDate date];
  30. NSDate *referenceDate;
  31. NSCalendar *calendar = [NSCalendar currentCalendar];
  32.  
  33. // get today's date components
  34. NSDateComponents *components = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:today];
  35.  
  36. // changing the date components to the 31nd of December of last year
  37. components.day = 31;
  38. components.month = 12;
  39. components.year--;
  40.  
  41. // store these components in your date object
  42. referenceDate = [calendar dateFromComponents:components];
  43.  
  44. // get the number of days from that date until today
  45. components = [calendar components:NSDayCalendarUnit fromDate:referenceDate toDate:[NSDate date] options:0];
  46. NSInteger days = components.day;
Add Comment
Please, Sign In to add comment