LoginSignup
0
0

More than 5 years have passed since last update.

日付の計算で迷惑な仕様と出くわしてハマった件(犯人は YYYY)

Posted at

有効期限などを計算する際に現在日時の1年後を計算することがよくあると思います。

その際の計算方法としてNSCalendarのdateByAddingComponentsを使用すればよいと思っていましたが、場合によって計算がうまくいかない場合がありました。

    NSDateComponents* com = [NSDateComponents new];
    [com setYear:2018];
    [com setMonth:12];
    [com setDay:29];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate* baseDay = [calendar dateFromComponents:com];
    NSDateComponents *date= [[NSDateComponents alloc] init];
    date.year= 1;
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setCalendar:calendar];
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];
    fmt.timeZone = [NSTimeZone systemTimeZone];
    fmt.dateFormat = @"YYYY/MM/dd";
    NSDate *expireDate = [calendar dateByAddingComponents:date toDate:baseDay options:0];
    NSSTring *strExpireDate = [NSString stringWithFormat:@"%@",[fmt stringFromDate:expireDate]];

↑の実装であればstrExpireDateは2019/12/29になってほしいところですが、実際は2020/12/29になります。

犯人は「YYYY」

いろいろ試して特定の年月日でずれることがわかったので、きっと計算の仕方でなにか書き方が間違っているはずと試して、いろいろな方が書いてある方法を試しているうちにふと気づきました。
「YYYY?あれここって大文字だっけ?」
調べてみるとNSDateFormatter.dateFormatにおける「年」において
「YYYY」「yyyy」はそれぞれ定義されており、結論としてはここを修正することでうまくいきました。

    NSDateComponents* com = [NSDateComponents new];
    [com setYear:2018];
    [com setMonth:12];
    [com setDay:29];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate* baseDay = [calendar dateFromComponents:com];
    NSDateComponents *date= [[NSDateComponents alloc] init];
    date.year= 1;
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setCalendar:calendar];
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];
    fmt.timeZone = [NSTimeZone systemTimeZone];
    fmt.dateFormat = @"yyyy/MM/dd"; //←ここを修正
    NSDate *expireDate = [calendar dateByAddingComponents:date toDate:baseDay options:0];
    NSSTring *strExpireDate = [NSString stringWithFormat:@"%@",[fmt stringFromDate:expireDate]];

もともとはteratailにも質問しましたが、結局自己解決してしまったので一応元ネタとしてリンクを貼ります。

元記事
https://teratail.com/questions/168328
参考
http://d.hatena.ne.jp/nakamura001/20150102/1420213850

0
0
0

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
0
0