剩下的时间直到日期(午夜?)

时间:2012-08-16 16:36:16

标签: iphone objective-c ipad time nsdate

我有一些代码可以解决从今天到给定日期的时间,这个日期是用户从日期选择器中选择的日期。

我将在下面发布代码,但首先是我的问题。我有一个标签,用于更新所选日期之前的月,日,小时等。然而,当它下降到所选日期的前一天时,它变得奇怪。

我会假设(可能是错误的)并希望它倒计时到午夜,所以一旦选定的那天,我就把我的标签改为我喜欢的任何东西。相反,它表示在所选日期午夜前约4小时还剩24小时。

我只想让它倒数到午夜。

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];
        }
    }

2 个答案:

答案 0 :(得分:2)

原来我需要将日期小时,分钟和秒重置为0,以便日期是我保存的午夜。

因此,当我从日期选择器中保存日期时,我会这样做:

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];

所以现在我选择了将小时,分钟和秒设置为午夜的日期,因此倒计时会运行到正确的时间。

在此处找到解决方案:

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

答案 1 :(得分:0)

Keith Lazuka几乎有相同的解决方案:

NSDateAdditions.hNSDateAdditions.m

但它还包含其他有用的方法:

- (NSDate *)cc_dateByMovingToBeginningOfDay;
- (NSDate *)cc_dateByMovingToEndOfDay;
- (NSDate *)cc_dateByMovingToFirstDayOfTheMonth;
- (NSDate *)cc_dateByMovingToFirstDayOfThePreviousMonth;
- (NSDate *)cc_dateByMovingToFirstDayOfTheFollowingMonth;
- (NSDateComponents *)cc_componentsForMonthDayAndYear;
- (NSUInteger)cc_weekday;
- (NSUInteger)cc_numberOfDaysInMonth;