timeIntervalSinceDate返回0

时间:2011-11-23 10:48:01

标签: objective-c

我试图知道日期是否介于其他2之间。棘手的是,这些日期只是由工作日和时间组成。为了做到这一点,并跳过以前的问题,我采取了第一个工作日,那些是1970年1月。所以我采取当前的工作日从1970年开始,如下: NSDate * dateNow = [NSDate date];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en"] autorelease]];
    [dateFormatter setDateFormat:@"EEEE HH:mm"];    

    NSString *dateString = [dateFormatter stringFromDate:dateNow];

    NSLog(@"La fecha es: %@", dateString);

    NSDate *dateComp =  [dateFormatter dateFromString:dateString];//Date and time in 1970

我将此日期与修正日期(工作日和时间)进行比较,并在任何时间间隔内查看它,如下所示:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en"] autorelease]];
[dateFormatter setDateFormat:@"EEEE HH:mm dd-MM-y"];    
NSString *dateString = [dateFormatter stringFromDate:self.fechaInicio];

NSLog(@"La date start es: %@", dateString);

dateString = [dateFormatter stringFromDate:self.fechaFin];

NSLog(@"La Date end es: %@", dateString);
dateString = [dateFormatter stringFromDate:testDate];

NSLog(@"DAte inside es: %@", dateString);

NSLog(@"time interval nicio: %d", [testDate timeIntervalSinceDate:self.fechaInicio]);
NSLog(@"time interval fin: %d", [testDate timeIntervalSinceDate:self.fechaFin]);

但这是输出

Palyque[746:307] La date start es: Monday 09:00 05-01-1970
2011-11-23 11:34:47.857 Palyque[746:307] La Date end es: Monday 16:00 05-01-1970
2011-11-23 11:34:47.861 Palyque[746:307] DAte inside es: Wednesday 11:34 07-01-1970
2011-11-23 11:34:47.864 Palyque[746:307] time interval nicio: 0 
2011-11-23 11:34:47.867 Palyque[746:307] time interval fin: 0

有什么想法吗?为什么总是0?我在计算timeIntervalSinceDate之前打印日期,它看起来很好......

1 个答案:

答案 0 :(得分:2)

NSTimeInterval是一个浮点数。您必须使用%f格式说明符来记录这些值。