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

  • SEARCH
  • Home
  • 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 7988789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:33:35+00:00 2026-06-04T12:33:35+00:00

I have 3 variables; startDate – Wed Apr 12 endDate – Wed Jun 11

  • 0

I have 3 variables;

startDate – Wed Apr 12
endDate – Wed Jun 11
timeDuration – 11.00 am from 11.00 pm. (this is a string, 11AM is the start time and 12PM is the end time)

1.) Now, i need to get the current date and time ( [NSDate date] ) and check if the current date is between the startDate and endDate and then check if the current time is between the timeDuration (as in between 11.00am from 11.00pm)

2.) If the current time is between the time duration (as in between 11.00 am from 11.00 pm) I need to calculate the time remaining to the following scenarios;

a.) If the time is 10am, i need to calculate the time to the start time which is 11.00am. (the answer is 1 hour).

b.) If the time is 11.45AM, since the time is greater than the start time (which was 11.00am) i need to calculate the time remaining to the end time (which is 11.00pm).

c.) If the time is 11.45pm, since the time is greater than the end time (which is 11.00pm), i need to print a NSLog saying that ‘Time Exceeded’.

  • 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-06-04T12:33:37+00:00Added an answer on June 4, 2026 at 12:33 pm

    The basic idea is to convert your startDate and endDate into actual NSDate‘s and compare them to the current date. Then if the current date is in the proper range, you add the start and end times onto the current date to come up with a specific point in time for the starting and ending times. Once you have those dates, you can use the compare and timeIntervalSinceDate methods of NSDate to get the values that you need:

    // NOTE:  NO error checking is being done in this code.
    
    // Starting variables  
    NSString *startDateString     = @"Wed Apr 12";
    NSString *endDateString       = @"Wed Jun 11";
    NSString *timeDurationString = @"11.00 am from 11.00 pm";
    
    // Get the current date
    NSDate *currentTime        = [NSDate date];
    NSCalendar *cal            = [NSCalendar currentCalendar];
    NSDateComponents 
             *currentDateComps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
                                        fromDate:currentTime];
    
    // Add the current year onto our date string
    NSString *startDateStringWithYear = [NSString stringWithFormat:@"%@ %d", startDateString, currentDateComps.year];
    NSString *endDateStringWithYear   = [NSString stringWithFormat:@"%@ %d", endDateString, currentDateComps.year];
    
    // Calculate NSDate's for start/endDate
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEE MM dd yyyy"];
    NSDate *startDate = [formatter dateFromString:startDateStringWithYear];
    NSDate *endDate   = [formatter dateFromString:endDateStringWithYear];
    
    // Return if today's date is not between the start and end dates
    if ([startDate compare:currentTime] == NSOrderedDescending || 
        [endDate compare:currentTime] == NSOrderedAscending) 
    {
        return;
    }
    
    // Break the timeDurationString into separate words
    NSArray *timeDurationStringWords = [timeDurationString componentsSeparatedByString:@" "];
    
    // Calculate NSDate's for the start time for today:
    NSString *startTimeString = [timeDurationStringWords objectAtIndex:0];
    currentDateComps.hour     = [[startTimeString substringToIndex:2] intValue];
    currentDateComps.minute   = [[startTimeString substringFromIndex:3] intValue];
    if ([[timeDurationStringWords objectAtIndex:1] isEqualToString:@"pm"]) 
    {
        currentDateComps.hour += 12;
    }
    NSDate *startTime         = [cal dateFromComponents:currentDateComps];
    
    // And now the end time for today
    NSString *endTimeString   = [timeDurationStringWords objectAtIndex:3];
    currentDateComps.hour     = [[endTimeString substringToIndex:2] intValue];
    currentDateComps.minute   = [[endTimeString substringFromIndex:3] intValue];
    if ([[timeDurationStringWords objectAtIndex:4] isEqualToString:@"pm"]) 
    {
        currentDateComps.hour += 12;
    }
    NSDate *endTime           = [cal dateFromComponents:currentDateComps];
    
    // Now we have what we really want:  A specific start time, current time, and end time.
    
    // Check to see if we are waiting to start:
    if ([startTime compare:currentTime] == NSOrderedDescending) {
        NSTimeInterval minutesToStartTime = [startTime timeIntervalSinceDate:currentTime] / 60;
        NSLog(@"Start time is in %02d+%02d", (int)(minutesToStartTime / 60), (int)minutesToStartTime % 60);
        return;
    }
    
    // Check to see if we are late:
    if ([endTime compare:currentTime] == NSOrderedAscending) {
        NSLog(@"Time Exceeded");
        return;
    }
    
    // We are between the two times:
    NSTimeInterval minutesToEndTime = [endTime timeIntervalSinceDate:currentTime] / 60;
    NSLog(@"End time is in %02d+%02d", (int)(minutesToEndTime / 60), (int)minutesToEndTime % 60);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these variables: (not literally, but this is what I obtain from a
I have two variables. weekStartDate and startDate. they both hold essentially the same timestamp:
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
I have a table with data exactly like this: ID Heading Description Time Location
I have this class: @interface Mission : BaseModel { NSString *Description; NSDate *EndDate; NSMutableArray
I have variables with values like 1.7m 1.8k and 1.2b how can I convert
I have two variables, key and value , and I want to add them
i have following variables in jquery var uemail=abc@domain.com,xyz@domain.com,gty@domain.com; var subj=test message; var text=<b>This is
I have some variables inside a template and I don't know where I assigned
I have two variables: unsigned short a,b; /* When I compare them with a

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.