【发布时间】:2012-08-13 01:44:16
【问题描述】:
我有一些代码可以计算出从今天到给定日期的时间,这个日期是用户从日期选择器中选择的日期。
我将在下面发布代码,但首先是我的问题。我有一个标签,它会根据所选日期之前的月份、日期、小时数等进行更新。但是,当它下降到所选日期的前一天时,它会变得很奇怪。
我会假设(可能是错误的)并希望它倒计时到午夜,所以一旦选定的日期,我将我的标签更改为我喜欢的任何内容。相反,它表示距离所选日期的午夜还有 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];
}
}
【问题讨论】:
-
你能详细说明一下这个行为吗?它在前一天之前是否正常运行,最后一天你看到了什么?
-
例如,可能是最后一天晚上 8 点,它会显示还剩 22 小时或超过应有的时间。无论如何,我发现这是因为日期选择器没有给出带有午夜的日期,而是给出了其他一些小时值,所以在保存日期时我将小时部分设置为午夜。
-
酷。也许您可以详细说明您的解决方案并接受它=)
-
可以,不过两天接受不了!
标签: iphone objective-c ipad time nsdate