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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:09:31+00:00 2026-05-23T02:09:31+00:00

Alright I’ve given up on this. Here’s what I’m trying to do: I have

  • 0

Alright I’ve given up on this. Here’s what I’m trying to do: I have a sunrise, sunset, and the current time in a certain timezone. I want to know if it’s day or night by figuring out if the current time lies between the sunrise and the sunset times.

Here’s what I have:

NSLog(@"%@ - %@ - %@",currTime,sunrise,sunset);

NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter1 setDateFormat:@"hh:mm a"];
[formatter2 setDateFormat:@"EEE, dd MMM yyyy h:mm a z"];
NSDate *rise = [formatter1 dateFromString:sunrise];
NSDate *set = [formatter1 dateFromString:sunset];
NSDate *time = [formatter2 dateFromString:currTime];    
[formatter1 release];
[formatter2 release];

unsigned int flags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components1 = [calendar components:flags fromDate:rise];
NSDateComponents *components2 = [calendar components:flags fromDate:set];
NSDateComponents *components3 = [calendar components:flags fromDate:time];

NSDate *Sunrise = [calendar dateFromComponents:components1];
NSDate *Sunset = [calendar dateFromComponents:components2];
NSDate *Time = [calendar dateFromComponents:components3];

NSLog(@"\nSunrise: %@ \nSunset:%@ \nTime:%@",rise,set,time);
NSLog(@"\nSunrise: %@ \nSunset:%@ \nTime:%@",Sunrise,Sunset,Time);

Here’s the first output:

Fri, 10 Jun 2011 4:00 am SAST – 7:46 am – 5:41 pm

And here’s the second (before making it only concerned about the time, not date)

Sunrise: 1969-12-31 22:46:00 +0000

Sunset: 1970-01-01 08:41:00 +0000

Time: 2011-06-10 02:00:00 +0000

And finally here is the last output (notice how the times are messed up?):

Sunrise: 0001-12-31 22:27:01 +0000

Sunset: 0001-01-01 08:22:01 +0000

Time: 0001-01-01 01:41:01 +0000

So I wanted to pop those resulting dates into my method that checks whether it’s in between the dates:

+(BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate {
    return (([date compare:beginDate] != NSOrderedAscending) && ([date compare:endDate] != NSOrderedDescending));
}

However, until I get the date problem figured out that method won’t work. :/ I need help! What am I doing wrong?


Ok, so I gave up on trying to get NSDates to work for me. The timezone issues just killed my brain for the weekend. Anyway, I decided to use BoopMeister suggestion, but it doesn’t work quite like I expect. Here’s an example:

Using the setup from above, I added these lines:

NSInteger riseHour = [components1 hour];
NSInteger setHour = [components2 hour];
NSInteger timeHour = [components3 hour];

NSLog(@"Rise: %i  Set: %i  Time: %i",riseHour,setHour,timeHour);

Now, when I plug in these variables:
Current time: Fri, 10 Jun 2011 9:07 am CDT
Sunrise: 6:33 am
Sunset: 8:32 pm

However, when I output the strings from the methods above here’s what I get:

Rise: 6 Set: 20 Time: 23

What the?

  • 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-23T02:09:31+00:00Added an answer on May 23, 2026 at 2:09 am

    I would use the components you already have and not make new dates.

    Starting at this point in your code:

    NSDateComponents *riseComponents = [calendar components:flags fromDate:rise];
    NSDateComponents *setComponents = [calendar components:flags fromDate:set];
    NSDateComponents *timeComponents = [calendar components:flags fromDate:time];
    

    And then something like

    NSInteger riseHour = [riseComponents hour];
    NSInteger setHour = [setComponents hour];
    NSInteger timeHour = [timeComponents hour];
    
    // Do some checks here
    // If necessary do the same for the minutes ([components minute])
    

    Comparing dates has been a performance issue in my app and since you already have the dateComponents it would be faster to make your own check and use the NSIntegers.


    Okay, so as can be seen in the question, it gives the numerical presentation of the hours. Same works for the minutes. Build your checks after that.

    What probably is the problem with the current time, is the calendar you use. It automatically converts the time to the time in the timezone of the calendar you use. You can also create a calendar with a string representation of the timezone. It’s in the API of NSCalendar I think. Then after you made that calendar, then use that one for the current time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I'm having a hard time explaining this, let me know if I should
Alright, here is my problem i'm trying to solve. I have an index page
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, hard to phrase an exact title for this question, but here goes... I
Alright, i dont know how to explain it well.. but i have a switch
Alright, i have read many different views on how to do this with no
Alright, here's what I'm trying to do. I'm attempting to write a quick build
Alright here is what I am attempting to do. I have a form written
alright, I'm trying something which should be like really easy. Here it is: usort($newarr,
Alright, this is basic c++ here. I've got a class that is a Linear

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.