強火で進め

このブログではプログラム関連の記事を中心に書いてます。

NSDate - 日付の計算、過去の日や末日を求める方法

日付計算のうちよく使う可能性の高いものを紹介。

オフセット付きのNSDate生成

NSDate生成時に現在の時刻に指定秒のオフセットを加え、過去や未来の時刻を指定する。

	NSDate* date;

	date = [NSDate dateWithTimeIntervalSinceNow:0.0f]; // 現在の時刻 
	NSLog(@"現在の時刻 -> %@", date);

	date = [NSDate dateWithTimeIntervalSinceNow:3*60]; // 3分後
	NSLog(@"3分後 -> %@", date);

	date = [NSDate dateWithTimeIntervalSinceNow:3*60*60]; // 3時間後
	NSLog(@"3時間後 -> %@", date);

	date = [NSDate dateWithTimeIntervalSinceNow:3*24*60*60]; // 3日後
	NSLog(@"3日後 -> %@", date);

	date = [NSDate dateWithTimeIntervalSinceNow:-5*24*60*60]; // 5日前(マイナスを指定する事で過去に)
	NSLog(@"5日前(マイナスを指定する事で過去に) -> %@", date);

出力

現在の時刻 -> 2010-06-10 01:29:02 +0900
3分後 -> 2010-06-10 01:32:02 +0900
3時間後 -> 2010-06-10 04:29:02 +0900
3日後 -> 2010-06-13 01:29:02 +0900
5日前(マイナスを指定する事で過去に) -> 2010-06-05 01:29:02 +0900

NSDateにオフセットを加えたものをNSDateで取得

	NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
	[inputDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
	NSDate *orgDate = [inputDateFormatter dateFromString:@"2000/01/02 00:00:00"];
	NSLog(@"設定した時刻 -> %@", orgDate);
	NSDate *resDate;

	resDate = [orgDate initWithTimeInterval:3*60 sinceDate:orgDate]; // orgDateから3分後
	NSLog(@"3分後 -> %@", resDate);

	resDate = [orgDate initWithTimeInterval:3*60*60 sinceDate:orgDate]; // orgDateから3時間後
	NSLog(@"3時間後 -> %@", resDate);

	resDate = [orgDate initWithTimeInterval:3*24*60*60 sinceDate:orgDate]; // orgDateから3日後
	NSLog(@"3日後 -> %@", resDate);

	resDate = [orgDate initWithTimeInterval:-5*24*60*60 sinceDate:orgDate]; // orgDateから5日前(マイナスを指定する事で過去に)
	NSLog(@"5日前 -> %@", resDate);
	[inputDateFormatter release];

出力

設定した時刻 -> 2000-01-02 00:00:00 +0900
3分後 -> 2000-01-02 00:03:00 +0900
3時間後 -> 2000-01-02 03:00:00 +0900
3日後 -> 2000-01-05 00:00:00 +0900
5日前 -> 1999-12-28 00:00:00 +0900

指定の月の末日を求める

	// 2000年の2月の末日を求める(翌月の1日の1日前で計算)
	NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
	[inputDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
	NSDate *orgDate = [inputDateFormatter dateFromString:@"2000/03/01 00:00:00"];
	NSDate *resDate = [orgDate initWithTimeInterval:-1*24*60*60 sinceDate:orgDate]; // 1日前
	NSLog(@"2000年の2月の末日 -> %@", resDate);
	[inputDateFormatter release];

出力

2000年の2月の末日 -> 2000-02-29 00:00:00 +0900

過去、未来の比較

2つのNSDateのうち、より過去のものや未来のものを返す。

	NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
	[inputDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
	NSDate *dateA = [inputDateFormatter dateFromString:@"2000/03/01 00:00:00"];
	NSDate *dateB = [inputDateFormatter dateFromString:@"2000/03/03 00:00:00"];

	// 2つの日付のうち、より過去の日を返す
	NSLog(@"%@, %@ -> %@", dateA, dateB, [dateA earlierDate:dateB]);

	// 2つの日付のうち、より未来の日を返す
	NSLog(@"%@, %@ -> %@", dateA, dateB, [dateA laterDate:dateB]);
	[inputDateFormatter release];

出力

2000-03-01 00:00:00 +0900, 2000-03-03 00:00:00 +0900 -> 2000-03-01 00:00:00 +0900
2000-03-01 00:00:00 +0900, 2000-03-03 00:00:00 +0900 -> 2000-03-03 00:00:00 +0900

2つの時刻の差を取得

	NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
	[inputDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
	NSDate *dateA = [inputDateFormatter dateFromString:@"2000/03/01 00:00:00"];
	NSDate *dateB = [inputDateFormatter dateFromString:@"2000/03/03 00:00:00"];
	NSTimeInterval since;

	// dateAとdateBの時間の間隔を取得(dateA - dateBなイメージ)
	since = [dateA timeIntervalSinceDate:dateB];
	NSLog(@"%f秒", since);
	NSLog(@"%f分", since/60);
	NSLog(@"%f時", since/(60*60));
	NSLog(@"%f日", since/(24*60*60));

	// dateBとdateAの時間の間隔を取得(dateB - dateAなイメージ)
	since = [dateB timeIntervalSinceDate:dateA];
	NSLog(@"%f秒", since);
	NSLog(@"%f分", since/60);
	NSLog(@"%f時", since/(60*60));
	NSLog(@"%f日", since/(24*60*60));
	[inputDateFormatter release];

出力

-172800.000000秒
-2880.000000分
-48.000000時
-2.000000日
172800.000000秒
2880.000000分
48.000000時
2.000000日

今日の日付のみ(時分を含まない)を取得

	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	
	NSString *nowDateStr = [dateFormatter stringFromDate:[NSDate date]];
	NSDate *nowDate = [dateFormatter dateFromString:nowDateStr];
	NSLog(@"%@", nowDate);
	[dateFormatter release];

出力

2010-06-10 00:00:00 +0900
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy