Skip to content

Instantly share code, notes, and snippets.

@edom18
Created March 24, 2014 14:05
Show Gist options
  • Select an option

  • Save edom18/9740746 to your computer and use it in GitHub Desktop.

Select an option

Save edom18/9740746 to your computer and use it in GitHub Desktop.
[Objective-C] NSDate周りのまとめメモ ref: http://qiita.com/edo_m18/items/5c7c901238e82afac50d
// 今の時間
NSDate *now = [NSDate date];
// 今から3秒後
NSDate *after3 = [NSDate dateWithTimeIntervalSinceNow:3.0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// フォーマットを文字列で指定
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
// 文字列からNSDateオブジェクトを生成
NSDate *fromFormatDate = [dateFormatter dateFromString:@"2014/03/18 00:00:00"];
// 特定の日付から未来の時間(5分後)
NSDate *futerDate = [fromFormatDate initWithTimeInterval:(5 * 60) sinceDate:fromFormatDate];
NSDate *now = [NSDate date];
NSDate *otherDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
NSComparisonResult result = [now compare:otherDate];
switch (result) {
case NSOrderedSame:
// 同一時刻
break;
case NSOrderedAscending:
// nowよりotherDateのほうが未来
break;
case NSOrderedDescending:
// nowよりotherDateのほうが過去
break;
}
// anyDateという、以前取得したデータがあるとする
NSDate *now = [NSDate date];
NSTimeInterval delta = [now timeIntervalSinceDate:anyDate]; // => 例えば 500.0 秒後
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment