Objective-C 日付処理

今更ですが、Objective-Cでの日付処理をメモ


		NSCalendar* cal = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
		NSDateComponents* comp;
		NSDate *today = [NSDate date];

		// 今日
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
		[comp setYear:comp.year];
		[comp setMonth:comp.month];
		[comp setDay:comp.day];
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[cal dateFromComponents:comp]];
		startDate =	[NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)comp.year, (long)comp.month, (long)comp.day];
		endDate = startDate;
//		NSLog(@"%@ %@",startDate,endDate);

		// 昨日
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
		[comp setYear:comp.year];
		[comp setMonth:comp.month];
		[comp setDay:comp.day -1];
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[cal dateFromComponents:comp]];
		startDate =	[NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)comp.year, (long)comp.month, (long)comp.day];
		endDate = startDate;
//		NSLog(@"%@ %@",startDate,endDate);

		// 過去1週間
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
		[comp setYear:comp.year];
		[comp setMonth:comp.month];
		[comp setDay:comp.day -1];
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[cal dateFromComponents:comp]];
		endDate =	[NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)comp.year, (long)comp.month, (long)comp.day];
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
		[comp setYear:comp.year];
		[comp setMonth:comp.month];
		[comp setDay:comp.day -8];
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[cal dateFromComponents:comp]];
		startDate =	[NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)comp.year, (long)comp.month, (long)comp.day];
//		NSLog(@"%@ %@",startDate,endDate);

		// 今月
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
		startDate =	[NSString stringWithFormat:@"%04ld-%02ld-01", (long)comp.year, (long)comp.month];
		endDate =	[NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)comp.year, (long)comp.month, (long)[cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:today].length];
//		NSLog(@"%@ %@",startDate,endDate);

		// 先月
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:today];
		[comp setYear:comp.year];
		[comp setMonth:comp.month - 1];
		[comp setDay:comp.day];
		NSDate *lastMonth = [cal dateFromComponents:comp];	// 先月
		comp = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:lastMonth];
		[comp setYear:comp.year];
		[comp setMonth:comp.month];
		[comp setDay:1];
		lastMonth = [cal dateFromComponents:comp];			// 先月1日
		startDate =	[NSString stringWithFormat:@"%04ld-%02ld-01", (long)comp.year, (long)comp.month];
		endDate =	[NSString stringWithFormat:@"%04ld-%02ld-%02ld", (long)comp.year, (long)comp.month, (long)[cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:today].length];
//		NSLog(@"%@ %@",startDate,endDate);

この記事が気に入ったら
いいねしてね!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

目次