NSDateComponents:查找超过30天

时间:2015-06-29 09:12:48

标签: objective-c nsdatecomponents date-comparison

使用NSDateComponents我想将出发日期与现在进行比较,以确定出发日期是否为> 30天。

我使用NSDateComponents功能,但在调试时,它总是像一个非常大的数字,当我预计它是3天,或15天或其他。

   NSDate* now = [NSDate date];

    NSUInteger unitFlags = NSDayCalendarUnit;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:departureDate toDate:now options:0];

    NSInteger age = [components day]+1;

当我尝试记录年龄时

NSLog(@"age (in days) = %lu", (long) age);

它会返回一个非常长的数字,例如18446744073709551613

我想做一个if语句;

if (age > 30)但是当返回的年龄是这样的长数时,我不确定如何做到这一点。

非常感谢

2 个答案:

答案 0 :(得分:1)

NSDate * dateNotFormatted = [NSDate date];
double now = [dateNotFormatted timeIntervalSince1970];
NSLog(@"%f",now);

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate * dateDepareture =[dateFormatter dateFromString:@"29-06-2015"];
double dateDept = [dateDepareture timeIntervalSince1970];
NSLog(@"%f",dateDept);

检查双倍值30天......

NSDateComponents *components;
NSInteger days;

components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:[NSDate date] toDate:[dateFormatter dateFromString:@"2-07-2015"] options:0];
days = [components day];
NSLog(@"%ld",(long)days);

将代码更改为您需要检查30天......

  

NSDayCalendarUnit NS_CALENDAR_ENUM_DEPRECATED(10_4,10_10,2_0,8_0,   “改为使用NSCalendarUnitDay”)= NSCalendarUnitDay

答案 1 :(得分:1)

public class ImagePagerAdapter extends PagerAdapter {
    private List<ImageView> images;

    public ImagePagerAdapter(List<ImageView> images) {
        this.images = images;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = images.get(position);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }

    @Override
    public int getCount() {
        return images.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object o) {
        // TODO Auto-generated method stub
        return view == o;
    }
}

这是你需要的吗?