Calculate Year From current NSDate

I want to calculate the year from current NSDate. Suppose my current date 14-06-2012 so, what would be last date after end of year. (In simple way I can explain you that, if I have date 01-01-2012 then after end of year I would get 31-12-2012). So, how do I calculate year form current date. Following output I expect for my project:

  1. Next or End year Date where year end (Like 31-12-2012).
  2. Next or end year month (Last date month).
  3. Next or end year (Last date year or next year).

It Should be NSDate format. Hope you would able to understand my point.

Answers


NSDate *today = [NSDate date]; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSYearCalendarUnit) fromDate:today];

[comps setDay:31]; // last day of year is always the same
[comps setMonth:12];

NSInteger year = [comps year]; // the current year
NSDate *lastDayOfYear = [gregorian dateFromComponents:comps]; // the last day of the year

You can use this:

NSDate *now = [NSDate date];
NSDateComponents *dc = [[NSDateComponents alloc] init];
[dc setYear:1];
NSDate *targetDateObject = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:now options:0];
[dc release];

Need Your Help

Web service failing with The request failed with HTTP status 401: Unauthorized

asp.net vb.net web-services iis-7 .net-4.0

I have a very long running task I need to take from my website and put into a web service however every time I try call the web service I get this error message The request failed with HTTP status ...

Recursive Searching and MySql Comparison

python mysql oop

Good evening. I am looking at developing some code that will collect EXIF data from JPEG images and store it in a MySQL database using Python v2.x The stumbling block lies in the fact that the JPEG...