Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当系统的日历不为公历,即使在App中设置为公历,时间显示还是会错误 #177

Open
newtypecxj opened this issue Feb 27, 2020 · 0 comments

Comments

@newtypecxj
Copy link

我在创建PGDatePicker时,设置了calendar和timeZone,但是在PGDatePicker+Common类的daysWithMonthInThisYear:withMonth:方法中,创建formatter后并未将calendar和timeZone设置进去。

  • (NSInteger)daysWithMonthInThisYear:(NSInteger)year withMonth:(NSInteger)month {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
    NSString *dateStr = [NSString stringWithFormat:@"%ld-%ld",year,month];
    NSDate *date = [formatter dateFromString:dateStr];
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
    inUnit: NSCalendarUnitMonth
    forDate:date];
    return range.length;

}
如果系统的日历不是公历,比如是佛历,当传入的year为2020,month为2,date得到的结果是1477-02-09,再转换到range,得到2月份只有28天,比实际少了1天。如果对函数做如下修改:

  • (NSInteger)daysWithMonthInThisYear:(NSInteger)year withMonth:(NSInteger)month {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.calendar = self.calendar;
    formatter.timeZone = self.timeZone;
    [formatter setDateFormat:@"yyyy-MM"];
    NSString *dateStr = [NSString stringWithFormat:@"%ld-%ld",year,month];
    NSDate *date = [formatter dateFromString:dateStr];
    NSRange range = [self.calendar rangeOfUnit:NSCalendarUnitDay
    inUnit: NSCalendarUnitMonth
    forDate:date];
    return range.length;

}
可以解决问题,但是我不知道是否会对其他地方产生影响。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant