pankaj
pankaj

Reputation: 8348

Number of day of current year in iOS

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 Jan01 of current year.

Upvotes: 2

Views: 3010

Answers (4)

SPA
SPA

Reputation: 1289

Use the ordinalityOfUnit method of the NSCalendar to get the day number in the year - specify the NSDayCalendarUnit inUnit:NSYearCalendarUnit

Swift

let calendar: Calendar = .autoupdatingCurrent
let dayOfTheYear = calendar.ordinality(of: .day, in: .year, for: Date())

Objective-C

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar  
    ordinalityOfUnit:NSDayCalendarUnit 
    inUnit:NSYearCalendarUnit 
    forDate:today
];

gives 269 for Sept 25th 2012

Upvotes: 14

Tim Vermeulen
Tim Vermeulen

Reputation: 12562

Using the NSDate, NSDateComponents and NSCalendar classes, you can quite easily calculate the amount of days between the last day of the previous year and today (which is the same as calculating today's number in the current year):

// create your NSDate and NSCalendar objects
NSDate *today = [NSDate date];
NSDate *referenceDate;
NSCalendar *calendar = [NSCalendar currentCalendar];

// get today's date components
NSDateComponents *components = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:today];

// changing the date components to the 31nd of December of last year
components.day = 31;
components.month = 12;
components.year--;

// store these components in your date object
referenceDate = [calendar dateFromComponents:components];

// get the number of days from that date until today
components = [calendar components:NSDayCalendarUnit fromDate:referenceDate toDate:[NSDate date] options:0];
NSInteger days = components.day;

Upvotes: 0

Caleb
Caleb

Reputation: 125007

According to the data format reference, you can use the D specifier to represent the day of the year. A date formatter isn't so helpful if you want to perform some calculations, but if you just want to display the day of year it's probably the easiest way to go. The code would look something like:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateFormatter *df = [[NSDateFormatter alloc] init];

[df setCalendar:cal];
[df setDateFormat:@"DDD"];    // D specifier used for day of year
NSString *dayOfYearString = [df stringFromDate:someDate];  // you choose 'someDate'

NSLog(@"The day is: %@", dayOfYearString);

Upvotes: 1

Zack Brown
Zack Brown

Reputation: 6028

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];

Upvotes: 2

Related Questions