Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:13:17+00:00 2026-05-13T13:13:17+00:00

Considering that there is no NSTime in Cocoa-Touch (Objective-C on iPhone), and given two

  • 0

Considering that there is no NSTime in Cocoa-Touch (Objective-C on iPhone), and given two times as NSStrings and a timezone as an NSString, how can you calculate whether or not the current LOCAL time is between these two times. Keep in mind that the date in the time strings do NOT matter, and are filled with dummy dates.

For example:

 TimeZone: Pacific Time (US & Canada)
 Start Time: 2000-01-01T10:00:00Z
 End Time: 2000-01-01T17:00:00Z

 Local Time: now

How do you confirm whether or not local time is between the time range specified (ensuring to convert the start/end times to the proper timezone first)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:13:18+00:00Added an answer on May 13, 2026 at 1:13 pm

    The biggest problem with this seems to come from the fact that the times may span two days (originally they might not, but when you do the timezone conversion, after that they might). So if we’re to ignore the given date information completely, some assumptions have to be made with how to handle these date spans. Your question isn’t exact on how to deal with this (i.e. I don’t know exactly what you’d like to achieve) so here’s just one way to go about it, which might not be exactly what you’re after, but hopefully it will guide you in the right direction:

    • Parse the given strings to NSDate objects, ignoring the date information (result: times are handled such that they’re assumed to be for the same day) and performing the time zone conversion
    • Get the time interval from the earlier NSDate to the later NSDate
    • Create NSDate objects for “today at the earlier given time” and “yesterday at the earlier given time”
    • Compare the time intervals from these two NSDates till the current date/time to the time interval between the two given date/times

    Also note that time zone strings in the format you gave ("Pacific Time (US & Canada)") will not be understood by NSTimeZone so you’ll need to do some conversion there.

    Here’s a code example (I wrote this on OS X since I don’t have the iPhone SDK so hopefully all the used APIs will be available on iPhone as well):

    - (BOOL)checkTimes
    {
        // won't work:
        //NSString *tzs = @"Pacific Time (US & Canada)";
        // 
        // will work (need to translate given timezone information
        // to abbreviations accepted by NSTimeZone -- won't cover
        // that here):
        NSString *tzs = @"PST";
    
        NSString *ds1 = @"2000-01-01T10:00:00Z";
        NSString *ds2 = @"2000-01-01T17:00:00Z";
    
        // remove dates from given strings (requirement was to ignore
        // the dates completely)
        ds1 = [ds1 substringFromIndex:11];
        ds2 = [ds2 substringFromIndex:11];
    
        // remove the UTC time zone designator from the end (don't know
        // what it's doing there since the time zone is given as a
        // separate field but I'll assume for the sake of this example
        // that the time zone designator for the given dates will
        // always be 'Z' and we'll always ignore it)
        ds1 = [ds1 substringToIndex:8];
        ds2 = [ds2 substringToIndex:8];
    
        // parse given dates into NSDate objects
        NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
        [df setDateFormat:@"HH:mm:ss"];
        [df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:tzs]];
        NSDate *date1 = [df dateFromString:ds1];
        NSDate *date2 = [df dateFromString:ds2];
    
        // get time interval from earlier to later given date
        NSDate *earlierDate = date1;
        NSTimeInterval ti = [date2 timeIntervalSinceDate:date1];
        if (ti < 0)
        {
            earlierDate = date2;
            ti = [date1 timeIntervalSinceDate:date2];
        }
    
        // get current date/time
        NSDate *now = [NSDate date];
    
        // create an NSDate for today at the earlier given time
        NSDateComponents *todayDateComps = [[NSCalendar currentCalendar]
                                            components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                            fromDate:now];
        NSDateComponents *earlierTimeComps = [[NSCalendar currentCalendar]
                                              components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
                                              fromDate:earlierDate];
        NSDateComponents *todayEarlierTimeComps = [[[NSDateComponents alloc] init] autorelease];
        [todayEarlierTimeComps setYear:[todayDateComps year]];
        [todayEarlierTimeComps setMonth:[todayDateComps month]];
        [todayEarlierTimeComps setDay:[todayDateComps day]];
        [todayEarlierTimeComps setHour:[earlierTimeComps hour]];
        [todayEarlierTimeComps setMinute:[earlierTimeComps minute]];
        [todayEarlierTimeComps setSecond:[earlierTimeComps second]];
        NSDate *todayEarlierTime = [[NSCalendar currentCalendar]
                                    dateFromComponents:todayEarlierTimeComps];
    
        // create an NSDate for yesterday at the earlier given time
        NSDateComponents *minusOneDayComps = [[[NSDateComponents alloc] init] autorelease];
        [minusOneDayComps setDay:-1];
        NSDate *yesterday = [[NSCalendar currentCalendar]
                             dateByAddingComponents:minusOneDayComps
                             toDate:now
                             options:0];
        NSDateComponents *yesterdayDateComps = [[NSCalendar currentCalendar]
                                                components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                                                fromDate:yesterday];
        NSDateComponents *yesterdayEarlierTimeComps = [[[NSDateComponents alloc] init] autorelease];
        [yesterdayEarlierTimeComps setYear:[yesterdayDateComps year]];
        [yesterdayEarlierTimeComps setMonth:[yesterdayDateComps month]];
        [yesterdayEarlierTimeComps setDay:[yesterdayDateComps day]];
        [yesterdayEarlierTimeComps setHour:[earlierTimeComps hour]];
        [yesterdayEarlierTimeComps setMinute:[earlierTimeComps minute]];
        [yesterdayEarlierTimeComps setSecond:[earlierTimeComps second]];
        NSDate *yesterdayEarlierTime = [[NSCalendar currentCalendar]
                                    dateFromComponents:yesterdayEarlierTimeComps];
    
        // check time interval from [today at the earlier given time] to [now]
        NSTimeInterval ti_todayEarlierTimeTillNow = [now timeIntervalSinceDate:todayEarlierTime];
        if (0 <= ti_todayEarlierTimeTillNow && ti_todayEarlierTimeTillNow <= ti)
            return YES;
    
        // check time interval from [yesterday at the earlier given time] to [now]
        NSTimeInterval ti_yesterdayEarlierTimeTillNow = [now timeIntervalSinceDate:yesterdayEarlierTime];
        if (0 <= ti_yesterdayEarlierTimeTillNow && ti_yesterdayEarlierTimeTillNow <= ti)
            return YES;
    
        return NO;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 273k
  • Answers 273k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can bind one-way, but then you'll have to update… May 13, 2026 at 2:09 pm
  • Editorial Team
    Editorial Team added an answer You shouldn't use JNI to copy files. If you are… May 13, 2026 at 2:09 pm
  • Editorial Team
    Editorial Team added an answer You can select which hunks to add in a commit… May 13, 2026 at 2:09 pm

Related Questions

Considering the fact that .net is a massive collection of classes, structs, interfaces, methods
I'm writng a small application in PHP + MySQL and have come to the
I have recently been reading about the general use of prime factors within cryptography.
I have been reading about out of memory conditions on Linux, and the following
I have a requirement to produce a list of possible duplicates before a user

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.