Question

Possible Duplicate:
How to get all the days/dates in current week, this month, this year using the current date (today) in iPhone

I need to get the first day of Month, first day of Year for a date

(Most of times input date is current date [NSDate date])

could you please suggest how to get it,

E.G. todays current date = 3-MAY-2015

Output needed as follows:

First_Date_month = 1-MAY-2015

First_Date_year = 1-JAN-2015

Thanks in advance for your time

Was it helpful?

Solution

you can find the day month and year using NSDateComponents and can make NSDate as you want

NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
[components setDay:1];
self.currentDate = [calendar dateFromComponents:components];
int m = components.month;
int y = components.year;
int d = components.day;


NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy MM dd"];
NSDate *firstDateOfMonth = [df dateFromString:[NSString stringWithFormat:@"%@ %@ 01",y,m]];
NSDate *firstDateOfYear = [df dateFromString:[NSString stringWithFormat:@"%@ 01 01",y]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top