1

IOS application to display Morning/Afternoon/Evening with logged in Name. eg: Good Morning Mr.X like this.

Need to calculate those event from current date and time zone

Rajesh M P
  • 477
  • 1
  • 10
  • 21
  • 3
    There are many posts on determining the time from the current time zone. I suggest searching the archives. – Jamie Jul 23 '13 at 04:29

2 Answers2

8
// 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
NSString *str = [dateFormatter stringFromDate:date];
NSLog(@"%@", str);

// Sperate str by space i.e. you will get time and AM/PM at index 0 and 1 respectively
NSArray *array = [str componentsSeparatedByString:@" "];

// Now you can check it by 12. If < 12 means Its morning > 12 means its evening or night

NSString *message;
NSString *personName = @"Mr.X";
NSString *timeInHour = array[0];
NSString *am_pm      = array[1];

if([timeInHour integerValue] < 12 && [am_pm isEqualToString:@"AM"])
{
    message = [NSString stringWithFormat:@"Good Morning %@", personName];
}
else if ([timeInHour integerValue] <= 4 && [am_pm isEqualToString:@"PM"])
{
    message = [NSString stringWithFormat:@"Good Afternoon %@", personName];
}
else if ([timeInHour integerValue] == 12 && [am_pm isEqualToString:@"PM"])
{
    message = [NSString stringWithFormat:@"Good Afternoon %@", personName];
}
else if ([timeInHour integerValue] > 4 && [am_pm isEqualToString:@"PM"])
{
    message = [NSString stringWithFormat:@"Good Night %@", personName];
}


NSLog(@"%@", message);

Using NSCalendar You can get hour as well:

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
}
Visastar
  • 1
  • 4
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • Why mess around parsing strings when you can use `NSCalendar` and `NSDateComponents` and get the hours directly. Then your code won't break if the current locale is using Arabic digits or something else. – benzado Jul 23 '13 at 04:44
  • 1
    Actually I haven't used `NSCalender` yet .. Obviously I didn't get any chance to work on it :( – TheTiger Jul 23 '13 at 04:47
  • Works fine, but I think the trick is to get Morning,Evening from date formatter rather then your own string? Then its much better with localization – Andrius Steponavičius Sep 18 '15 at 09:42
  • @AndriusSteponavičius Second method is correct and better because we are using `NSCalendar` but we can get only **am** and **pm** so there is a bit calculation for morning, noon and evening. – TheTiger Sep 18 '15 at 10:44
1

You could use 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
}
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84