How to get first date of MONTH, and first date of YEAR for a incoming date. in iPhone Objective c [duplicate]

StackOverflow https://stackoverflow.com/questions/14273524

Вопрос

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

Это было полезно?

Решение

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]];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top