尝试创建一个倒数计时器到某个特定日期,但我希望它能在几天内显示,而不是月,天(比如,2月2日)我宁愿让它显示实际数字像“62天”的日子我应该在代码中更改什么?感谢。
这是我用于CountDown的代码。
#import "CountdownViewController.h"
@interface CountdownViewController ()
@end
@implementation CountdownViewController;
-(void)updateLabel;
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
char units = NSDayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%dDays", [components day]]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
destinationDate = [NSDate dateWithTimeIntervalSince1970:1356393600];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
问题在于它只是向我展示了那些日子。我玩代码来到上面的代码,但它曾经像这样
-(void)updateLabel;
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
char units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d %d %d %d %d", [components month], [components day], [components hour], [components minute], [components second]]];
}
答案 0 :(得分:2)
只需将以下两种方法复制到您的类中,然后在viewDidLoad方法中调用启动计时器。您需要在getCountdownDate方法中更改所需的日期:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(getCountdownDate) userInfo:nil repeats: YES];
方法是:
#pragma mark --
#pragma mark CountDownTimmer takes one date input
-(void)getCountdownDate
{
NSString *date = @"2013-09-09 20:30:00";
NSDate *toDate = [self formatADateFromString:date];
NSLog(@"Date: %@", toDate);
NSString *remainingCountDown = [self countDownTimerToSpecificDate:toDate];
NSLog(@"%@", remainingCountDown);
}
-(NSString *)countDownTimerToSpecificDate:(NSDate*)toDateParameter
{
NSDate *toDate = toDateParameter;
NSDate *currentDate = [NSDate date];
NSLog(@"To Date: %@, Current Date: %@", toDate, currentDate);
int units;
int months, days, hour, minit, second_t;
NSDateComponents *components;
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
units = NSDayCalendarUnit;
components = [calender components:units fromDate:currentDate toDate:toDate options:0];
days = [components day];
days = days%30;
units = NSMonthCalendarUnit;
components = [calender components:units fromDate:currentDate toDate:toDate options:0];
months = [components day];
months = months%12;
//int totalSec = [components second];
//int month = [components month];
units = NSHourCalendarUnit;
components = [calender components:units fromDate:currentDate toDate:toDate options:0];
hour = [components hour];
hour = hour%24;
units = NSMinuteCalendarUnit;
components = [calender components:units fromDate:currentDate toDate:toDate options:0];
minit = [components minute];
minit = minit%60;
units = NSSecondCalendarUnit;
components = [calender components:units fromDate:currentDate toDate:toDate options:0];
second_t = [components second];
second_t = second_t%60;
NSString *returnString = [NSString stringWithFormat:@"%d Months, %d Days, %d Hours, %d Minitues, %d Seconds",months, days, hour, minit, second_t];
//NSLog(@"%@", returnString);
return returnString;
}
答案 1 :(得分:0)
之前,为您的标签提供价值,在本例中为dateLabel;你应该连接你的字符串,包括年,月,日或其他任何东西......
答案 2 :(得分:0)
尝试使用此方法从两个日期开始获取日期..
- (int)daysBetweenDates:(NSDate *)dt1:(NSDate *)dt2 {
int numDays;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:dt1 toDate:dt2 options:0];
numDays = [components day];
[gregorian release];
return numDays;
}
像下面这样使用它。
-(void)updateLabel;
{
int tempdays = [self daysBetweenDates:destinationDate :[NSDate date]];
[dateLabel setText:[NSString stringWithFormat:@"%dDays", tempdays]];
}