-2

重複の可能性:
iPhoneで現在の日付(今日)を使用して、今週、今月、今年のすべての日/日付を取得する方法

日付の月の最初の日、年の最初の日を取得する必要があります

(ほとんどの場合、入力日は現在の日付です[NSDate date])

入手方法を教えてください、

EG今日の現在の日付=3-MAY-2015

次のように出力が必要です。

First_Date_month =1-MAY-2015

First_Date_year =1-JAN-2015

お時間をいただきありがとうございます

4

1 に答える 1

3

NSDateComponents を使用して日月と年を見つけることができ、必要に応じて NSDate を作成できます

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]];
于 2013-01-11T07:30:08.583 に答える