Skip to content

Instantly share code, notes, and snippets.

@katsuhide
Created May 31, 2013 15:25
Show Gist options
  • Save katsuhide/5685732 to your computer and use it in GitHub Desktop.
Save katsuhide/5685732 to your computer and use it in GitHub Desktop.
時間まわりの処理
// 現在時間を取得
NSDate *now = [NSDate date];
NSLog(@"now:%@", [now toLocalTime]); // now:2013-06-01 00:19:46 +0000
// 現在時間にインターバル時間を足す
NSTimeInterval interval = 3600; // 秒
NSDate *after = [now dateByAddingTimeInterval:interval];
NSLog(@"after:%@", [after toLocalTime]); // after:2013-06-01 01:19:46 +0000
// 時間の判定
NSComparisonResult result = [now compare:after];
NSLog(@"result:%ld", result); // result:-1
result = [after compare:now];
NSLog(@"result:%ld", result); // result:1
result = [now compare:now];
NSLog(@"result:%ld", result); // result:0
// 文字列からNSDateへ
NSString *str = [dic objectForKey:@"datetime"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *messageTime = [formatter dateFromString:str];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment