I want to get a quarter value in NSDateComponents class

后端 未结 2 826
故里飘歌
故里飘歌 2020-12-04 02:15

I want to get a quarter value from specified date. (*date variable)

So. I have made following code and run. but, quarter value is zero.

Why quarter value is

相关标签:
2条回答
  • 2020-12-04 02:33

    It seems that there is an issue with the NSQuarterCalendarUnit being ignored (haven't verified, but did see a bug reported). Anyway, it is simple enough to get around... Just use the NSDateFormatter class.

    // After your NSDate *date, add...
    NSDateFormatter *quarterOnly = [[NSDateFormatter alloc]init];
    [quarterOnly setDateFormat:@"Q"];
    
    int quarter = [[quarterOnly stringFromDate:date] intValue];
    
    // Then change the quarter NSLog too
    NSLog(@"quarter :    %5d", quarter);
    

    Should work fine...

    0 讨论(0)
  • 2020-12-04 02:55

    Dec 16 2014

    How about simply:

    ([comp2 month]-1)/3+1;

    0 讨论(0)
提交回复
热议问题