因为遇到了这么个需求:
比如还款时间是:2017-12-06 到 2017-12-31, 然后页面需要显示pickerView, 里面的选项只能显示这些日期,因为网上有很多自定义pickView的demo,我就不重复了, 就直接说一下怎么获取这两个时间点之间的具体日期吧~
NSString *startStr = @"20171206";
NSString *endStr = @"20171231";
NSDateFormatter *formatterOld = [[NSDateFormatter alloc] init];
[formatterOld setDateFormat:@"yyyyMMdd"];
NSDate *startDate = [formatterOld dateFromString:startStr];
NSDate *endDate = [formatterOld dateFromString:endStr];
long long nowTime = [startDate timeIntervalSince1970],
endTime = [endDate timeIntervalSince1970],
dayTime = 24*60*60,
time = nowTime - nowTime % dayTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
while (time <= endTime) {
time += dayTime;
//!!!! dateWithTimeIntervalSince1970
NSString *string = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:time]];
NSLog(@"string = %@", string);
以上,没有什么逻辑需要过多解释的内容,就是那个方法不太好找,所以分享一下,大家可以试一下其他时间,如果发现bug,欢迎一起讨论