Created
March 24, 2014 14:05
-
-
Save edom18/9740746 to your computer and use it in GitHub Desktop.
[Objective-C] NSDate周りのまとめメモ ref: http://qiita.com/edo_m18/items/5c7c901238e82afac50d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 今の時間 | |
| NSDate *now = [NSDate date]; | |
| // 今から3秒後 | |
| NSDate *after3 = [NSDate dateWithTimeIntervalSinceNow:3.0]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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