21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Objective-C] 日付を操作する

Posted at

使うクラス

  • NSDate
  • NSCalendar
  • NSDateComponents

3日後の日付を得る

// 3日後
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comp = [[NSDateComponents alloc] init];
comp.day = 3;

NSDate *result = [calendar dateByAddingComponents:comp toDate:date options:0];
NSLog(@"3日後: %@", result);
3日後: 2014-08-28 23:53:46 +0000

TimeZone

ちなみに上記の出力を見てみると、+0000となっているのが分かります。
実は普通に計算すると「GMT(グリニッジ標準時間:日本と9時間差) の時刻」になってしまいます。
なのでローカルな時間に変換する必要があります。

それについては「NSDateの現在時刻を日本時間というかlocal時刻で」で書かれていました。

コードを引用させてもらうと、

NSDate* now = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone systemTimeZone] secondsFromGMT]];

とすればいいようです。

21
19
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?