0

I have an object with 2 String properties: notifyFrom and notifyTo.

They are like:

notifyFrom: 14:00

notifyTo: 18:00

and before I send the user a notification (that's part of my app) I want to check if the time is between those hours and just if it is to send the notification.

Note: Date doesn't matter here. I just want to check the times

What is the best way to do it?

Thanks!

FS.O6
  • 1,394
  • 2
  • 20
  • 42
  • means do you want to check that whether your time string is between `notifyfrom` and `notifyto` ?? If this is so then what about date ?? or date doesn't matter ?? – Ketan Parmar Oct 25 '16 at 08:23
  • @Lion Date doesn't matter here. I just want to check the times – FS.O6 Oct 25 '16 at 08:24
  • 1
    Get the `hour` component from the current date and check if the `Range` `notifyFrom...notifyTo` `contains` the value. However it becomes a bit more complicated if `To` can be lower than `From` – vadian Oct 25 '16 at 08:34

2 Answers2

0

You can write method something like,

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
if ([date compare:beginDate] == NSOrderedAscending)
    return NO;

if ([date compare:endDate] == NSOrderedDescending)
    return NO;

return YES;
}

Reference : this so post

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

First of all convert the input two strings to dates

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];

NSDate *date1 = [dateFormatter dateFromString:notifyFrom];
NSDate *date2 = [dateFormatter dateFromString:notifyTo];

NSDate *userDate = [NSDate date];

if (([userDate laterDate: date1] == userDate) && ([userDate laterDate: date2] == date2)){
}
Ali Omari
  • 371
  • 3
  • 11