0

I have a label I want to update daily and automatically? in some specific time. How could the code be.. I have searched every where about how to updating the label daily.

the NSdate and NStimer label working . And also datepicker if the user want to look at forward events.

I think it is something with, 'if' the date is.. }else{... and something like that

Thanks

edit:

If I already have these codes in my app then there will be something wrong

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];


NSDate *nowDate = [NSDate date];

self.currentDateLabel.text = [dateFormatter stringFromDate:nowDate];
  • Hey, you shouldnot post duplicate questions. You posted this same question yday no? Best practice is to edit the same question to a good form. – prince Jun 27 '13 at 08:48
  • I have edit it but it is in 'on hold', so nobody have seen it I think. Sorry – user2310932 Jun 27 '13 at 09:45

1 Answers1

0

Try this code for any comparison between dates... You should not compare date in the form of string. Compare the dates before conversion to string. Convert the self.serverDate into date format using dateFromString function of the formatter by specifying the exact date format as that of the dateString. Then compare the dates using following function.

-(void) callAfterSixtySecond:(NSTimer*) t 
{

NSDate *today = [NSDate date]; // current date
NSDate *newDate = self.serverDate; // other date 

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"HH:mm"];
NSDate *todayTime = [formatter dateFromString:[formatter stringFromDate:today]];
NSDate *requiredTime = [formatter dateFromString:[formatter stringFromDate:newDate]];

NSComparisonResult result; 

result = [todayTime compare:requiredTime ]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"today is less");
else if(result == NSOrderedDescending)
    NSLog(@"newDate is less");
else if(result == NSOrderedSame)
    NSLog(@"Both dates are same");  // time has reached. Update Label using setText method of label
else
    NSLog(@"Date cannot be compared");
}

You will need to run this method every minute using an NSTimer...

NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 60.0 target: self
                                   selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];
prince
  • 854
  • 2
  • 9
  • 36
  • yes it will... Try changing the time of your device and check. Also make sure that the fixed date you want to compare this with is in the format "HH:mm" ie 24 hour format. Else you will need to match the format accordingly.. – prince Jun 27 '13 at 09:15
  • setting locale is ok if your app works in different timezones. But the time format maybe a problem. You need to change the label every day at a particular time. But your format has only date, month and day. No time info is there. You need atleast the hour info ie "hh" or "HH" in the format to compare time. So all you need to do is substitute the terms in my answer with the function and parameter names in your function. – prince Jun 27 '13 at 12:54
  • Other than the comparison part, your code looks more or less the same as mine. So when result is NSOderedSame, you update the text label. – prince Jun 27 '13 at 12:56
  • In your timer, schedule the timer with time interval 60 (its in seconds) to check in every minute. Also you are simply setting current time to label without comparing with anything. You should order each method differently so we can distinguish between the methods. – prince Jun 27 '13 at 13:07