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 6883997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:27:11+00:00 2026-05-27T05:27:11+00:00

I am trying to have points in time and then compare them. I have

  • 0

I am trying to have points in time and then compare them. I have figured out how to compare them, but I am trying to create NSDate objects to represent times of day, and it is causing some trouble. I used NSDateFromComponents and used the setHour: and setMinute: functions, but it does not successfully modify the object. It also needs to automatically set the other components of the date to the current time and date.

COMPARING OBJECTS:

static bool DateIsBetween(NSDate *beginDate, NSDate *date, NSDate *endDate) {
    return [date timeIntervalSince1970] > [beginDate timeIntervalSince1970] && [date    timeIntervalSince1970] < [endDate timeIntervalSince1970];
}

CREATING DATE OBJECTS:

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:todayDate];

NSDateComponents *genericTimeComponents = [[NSDateComponents alloc]init];

[genericTimeComponents setHour:5];
[genericTimeComponents setMinute:0];
[genericTimeComponents setDay:weekdayComponents.day];
[genericTimeComponents setYear:weekdayComponents.year];
[genericTimeComponents setMonth:weekdayComponents.month];


NSDateComponents *secondComps = [[NSDateComponents alloc]init];
[secondComps setHour:7];
[secondComps setMinute:0];
[secondComps setDay:weekdayComponents.day];
[secondComps setYear:weekdayComponents.year];
[secondComps setEra:weekdayComponents.era];
[secondComps setMonth:weekdayComponents.month];
  • 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-27T05:27:11+00:00Added an answer on May 27, 2026 at 5:27 am

    Make sure to use parentheses in your date compare function (for the sake of clarity only; logically, it should work just fine):

     static bool DateIsBetween(NSDate *beginDate, NSDate *date, NSDate *endDate) {
        return (([date timeIntervalSince1970] > [beginDate timeIntervalSince1970]) && ([date    timeIntervalSince1970] < [endDate timeIntervalSince1970]));
    }
    

    For such cases, it’s a good idea to create a category for NSDate class and write an objective-c method.

    For the rest, I tried the following code and it worked for me:

    NSDate * now = [NSDate date];
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    
    [gregorian setTimeZone:[NSTimeZone localTimeZone]];
    
    NSDateComponents *genericTimeComponents = [gregorian components:(NSWeekdayCalendarUnit |NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSEraCalendarUnit)
                                                          fromDate:now];
    
    [genericTimeComponents setHour:5];
    [genericTimeComponents setMinute:0];
    
    //  Quick Check 
    NSLog(@"GTC=%@", genericTimeComponents);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to create a cursor for the first time. I have looked at
I have 8823 data points with x,y coordinates. I'm trying to follow the answer
I'm trying to put a bunch of (x,y) points on a map, that have
I'm trying to display some data points using google graphs, but unfortunately there is
I'am trying to construct a time series graph where the x-axes represent time and
I am trying to compare items out of my DB to the value of
Afternoon, I am trying have an HTML file containing a frameset which contains two
Trying to have my PHP script return some SQL table queries. Here's my script
I have trying to learn MVC4 Web API. I am running my project from
I have a cache directory I'm trying to have ignored by git which lives

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.