0

试图创建一个倒数计时器到某个日期,但我希望它以天而不是月、天显示事物(比如,2 个月 2 天)我宁愿让它显示实际天数,比如“62 天”我应该怎么做更改代码?谢谢。

这是我用于倒计时的代码。

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

3 回答 3

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;
}
于 2012-11-20T14:17:17.673 回答
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]];
}
于 2012-11-20T12:45:53.057 回答
0

之前,为您的标签提供价值,在本例中为 dateLabel;您应该连接包含年、月、日或其他任何内容的字符串...

于 2012-11-20T12:36:42.093 回答