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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:32:16+00:00 2026-06-09T22:32:16+00:00

I have some code which works out the time from today until a given

  • 0

I have some code which works out the time from today until a given date, this date being one the user has chosen from a date picker.

I will post the code below, but first my issue. I have a label which updates with the months, days, hours etc left until the selected date. However, when it gets down to the day before the selected date it goes weird.

I would have assumed (probably wrongly) and want it to countdown to midnight, so as soon as the selected day its, I change that my label to whatever I like. Instead, it says there is still 24 hours left about 4 hours prior to midnight of the selected date.

I just want it to countdown to midnight.

systemCalendar = [NSCalendar currentCalendar];
    unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    components = [systemCalendar components:unitFlags
                                   fromDate:[NSDate date]
                                     toDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"dateSaved"]
                                    options:0];

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"dateSaved"]) 
    {
        dateLbl.text = @"You haven't chosen a date yet.";
    }
    else
    {
        //Plural string variations
        NSString *seconds = @"Seconds";
        NSString *minutes = @"Minutes";
        NSString *hours = @"Hours";
        NSString *days = @"Days";
        NSString *months = @"Months";
        NSString *years = @"Years";

        //No time left until wedding day
        if ([components year] <= 0 && [components month] <= 0 && [components day] <= 0 && [components hour] <= 0 && [components minute] <= 0 && [components second] <= 0)
        {
            dateLbl.text = @"It's Your Day!";
        }
        //Hours left until day
        else if ([components year] <= 0 && [components month] <= 0 && [components day] <= 0 && ([components hour] > 0 || [components minute] > 0 || [components second] > 0))
        {
            if ([components hour] > 1) hours = @"Hours";
            else hours = @"Hour";

            if ([components minute] > 1) minutes = @"Minutes";
            else minutes = @"Minute";

            if ([components second] > 1) seconds = @"Seconds";
            else seconds = @"Second";

            dateLbl.text = [NSString stringWithFormat:@"%i %@, %i %@, %i %@ Until Your Day", [components hour], hours, [components minute], minutes, [components second], seconds];
            //dateLbl.text = @"Tomorrow is your day!";
        }
        //Days left until day
        else if ([components year] <= 0 && [components month] <= 0 && [components day] > 0)
        {
            if ([components day] > 1) days = @"Days";
            else days = @"Day";

            dateLbl.text = [NSString stringWithFormat:@"%i %@ Until Your Day", [components day], days];
        }
        //Months left until day
        else if ([components year] <= 0 && [components month] > 0)
        {
            if ([components month] > 1) months = @"Months";
            else months = @"Month";

            if ([components day] > 1) days = @"Days";
            else days = @"Day";

            dateLbl.text = [NSString stringWithFormat:@"%i %@, %i %@ Until Your Day", [components month], months, [components day], days];
        }
        //Years left until day
        else if ([components year] > 0)
        {
            if ([components year] > 1) years = @"Years";
            else years = @"Year";

            if ([components month] > 1) months = @"Months";
            else months = @"Month";

            if ([components day] > 1) days = @"Days";
            else days = @"Day";

            dateLbl.text = [NSString stringWithFormat:@"%i %@, %i %@, %i %@ Until Your Day", [components year], years, [components month], months, [components day], days];
        }
    }
  • 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-09T22:32:18+00:00Added an answer on June 9, 2026 at 10:32 pm

    Turns out I need to reset the date hours, minutes and seconds to 0, so that the date is at midnight when I save it.

    So when saving the date from my date picker I do this:

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSIntegerMax fromDate:[datePicker date]];
        //Reset date components
        [components setHour:0];
        [components setMinute:0];
        [components setSecond:0];
        //Create new date with components reset for midnight of selected day
        NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];
    

    So now I have the date which I selected with the hours, minute and seconds set to midnight, so the countdown runs to the correct time.

    Found the solution to do so here:

    How to retrieve number of hours past midnight from an NSDate object?

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

Sidebar

Related Questions

I have some code here which works perfectly in firefox but not in chrome
I have some code which builds an array of date ranges. I then call
I have some code to interface Python to C++ which works fine but every
I have some existing custom Excel workbooks/applications with code-behind C#, which work fine. They
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have some code which I want to document with in-body comments like so:
I have some code which I'm currently using to change the static-IP of a
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance

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.