Current date, convert to multiple timezones, then each needs to check if it is close to another date

261 Views Asked by At

I have a project that I need to get the current date/time. From that date/time I need to convert to 6 different timezone's date/time. That is no problem converting the times.

Then from each of the converted date/time I need to see if that date is between two other dates that change every week. I am guessing the times that change everyweek could be an array of dates. The current date/time in that timezone needs to find out which date/time in the array it needs to check itself against. The nearest before and the nearest after. This sounds very confusing just typing this.

Any help pointing me in the right direction would be extremely helpful.

Thankyou

2

There are 2 best solutions below

0
On
NSDate *date1;
NSDate *date2;
NSDate *date3;

NSTimeInterval firstTimeInterval = [date1 timeIntervalSince1970];
NSTimeInterval secondTimeInterval = [date2 timeIntervalSince1970];
NSTimeInterval thirdTimeInterval = [date3 timeIntervalSince1970];

if (firstTimeInterval<secondTimeInterval && secondTimeInterval<thirdTimeInterval) {
    // date2 > date1 and date2 < date3
}

Of course, in my case it would crash since dates have no addresses, but it's just an example... Yours would need to have actual dates in them.

0
On

To check if a date is in a specified range just get the unix timestamps and compare them directly like so:

NSDate *lowerLimit = ...
NSDate *upperLimit = ...
NSDate *myDate = ...

BOOL inRange = (lowerLimit.timeIntervalSince1970 <= myDate.timeIntervalSince1970) &&
  (upperLimit.timeIntervalSince1970 >= myDate.timeIntervalSince1970);

You could also use NSDate's -compare: method, but I think it's more natural to compare the timestamps.