Hesham Saeed
Hesham Saeed

Reputation: 5378

Add 90 min to NSDate

what is wrong with this code?

NSDate *matchDateCD = [[object valueForKey:@"matchDate"] description]; // from coredata NSDate
NSDate *add90Min = [matchDateCD dateByAddingTimeInterval:5400];



if ( matchDateCD >=[NSDate date] || add90Min <= matchDateCD )
{

    cell.imageView.image = [UIImage imageNamed: @"r.gif"];//Show image in the table

}    

I need to show this image in the table if the match is running or for 90 min

Upvotes: 18

Views: 20029

Answers (5)

Kiryl Bielašeŭski
Kiryl Bielašeŭski

Reputation: 2683

For Swift 4

To add 90 mins to Date use addingTimeInterval(_:)

let now = Date()
let minutes: Double = 90
let add90MinsDate = now.addingTimeInterval(minutes * 60)

For question, swift code below

if let matchDateCD: Date = objest.value(forKey: "matchDate") as? Date {
   let add90Min = matchDateCD.addingTimeInterval(90 * 60)
   if matchDateCD >= Date() || add90Min > matchDateCD {
        cell.imageView.image = UIImage(named: "r.gif")
   }
}

Upvotes: 3

Waseem Shah
Waseem Shah

Reputation: 2219

You also set Hour,Years,Months and seconds etc

     NSDateComponents *components= [[NSDateComponents alloc] init];
    [components setMinute:30];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *myNewDate=[calendar dateByAddingComponents:components toDate:[NSDate date] options:0];

Upvotes: 5

NSProgrammer
NSProgrammer

Reputation: 2396

I don't know what object is that you call valueForKey: on is but presuming it returns an NSDate object, your additional call to description will assign an NSString (the return value of description) to matchDateCD. That is not what you want to do.

This is what you want to do:

NSDate *matchDateCD = [object valueForKey:@"matchDate"];
NSDate *add90Min = [matchDateCD dateByAddingTimeInterval:(90*60)]; // compiler will precompute this to be 5400, but indicating the breakdown is clearer

if ( [matchDateCD earlierDate:[NSDate date]] != matchDateCD ||
     [add90Min laterDate:matchDateCD] == add90Min )
{
    cell.imageView.image = [UIImage imageNamed: @"r.gif"];//Show image in the table
}

Upvotes: 41

danh
danh

Reputation: 62686

Dates are objects, so it's no good to compare the pointers. Either convert them to common time intervals (floats):

NSDate *now = [NSDate date];
NSDate *tenMinsLater = [now dateByAddingTimeInterval:600];

NSTimeInterval nowInterval = [now timeIntervalSinceReferenceDate];
NSTimeInterval *tenMinsLaterInterval = [tenMinsLater timeIntervalSinceReferenceDate];

if (nowInterval > tenMinsLaterInterval) NSLog(@"never get here");

Or, use the comparators:

// also false under newtonian conditions
if (now > [now laterDate:tenMinsLater]) NSLog(@"einstein was right!");

or use earlierDate: or compare:

Upvotes: 1

SimplyKiwi
SimplyKiwi

Reputation: 12444

Use the dateByAddingTimeInterval method of NSDate to add the number of seconds to the time.

NSDate* newDate = [oldDate dateByAddingTimeInterval:90];

You can then use either NSDateFormatter or NSDateComponents to get the new time back out again.

Upvotes: 10

Related Questions