Demande d'affichage le matin, le soir

Application IOS à afficher Matin / Après-midi / Soir avec nom de connection. par exemple: Good Morning Mr.X comme ça.

Besoin de calculer ces events à partir de la date et du timezone actuels

// For calculating the current date NSDate *date = [NSDate date]; // Make Date Formatter NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh a"]; // hh for hour mm for minutes and a will show you AM or PM NSSsortingng *str = [dateFormatter ssortingngFromDate:date]; NSLog(@"%@", str); // Sperate str by space ie you will get time and AM/PM at index 0 and 1 respectively NSArray *array = [str componentsSeparatedBySsortingng:@" "]; // Now you can check it by 12. If < 12 means Its morning > 12 means its evening or night NSSsortingng *message; NSSsortingng *personName = @"Mr.X"; NSSsortingng *timeInHour = array[0]; NSSsortingng *am_pm = array[1]; if([timeInHour integerValue] < 12 && [am_pm isEqualToSsortingng:@"AM"]) { message = [NSSsortingng ssortingngWithFormat:@"Good Morning %@", personName]; } else if ([timeInHour integerValue] <= 4 && [am_pm isEqualToSsortingng:@"PM"]) { message = [NSSsortingng ssortingngWithFormat:@"Good Afternoon %@", personName]; } else if ([timeInHour integerValue] == 12 && [am_pm isEqualToSsortingng:@"PM"]) { message = [NSSsortingng ssortingngWithFormat:@"Good Afternoon %@", personName]; } else if ([timeInHour integerValue] > 4 && [am_pm isEqualToSsortingng:@"PM"]) { message = [NSSsortingng ssortingngWithFormat:@"Good Night %@", personName]; } NSLog(@"%@", message); 

Utilisation de NSCalendar Vous pouvez également get des heures:

 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [gregorian components:NSHourCalendarUnit fromDate:date]; NSInteger hour = [dateComponents hour]; if (hour < 12) { // Morning } else if (hour > 12 && hour <= 16) { // Afternoon } else { // Night } 

Vous pouvez utiliser NSDateComponents

 NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:[NSDate date]]; NSInteger currentHour = [components hour]; if (currentHour >= 0 && currentHour < 12) { //@"Good Morning } else if (currentHour >= 12 && currentHour < 17) { //Good Afternoon } else if (currentHour >= 17 && currentHour < 21) { //Good Evening } else { //"Good Night }