0

特定の日付までのカウントダウンタイマーを作成しようとしていますが、月、日ではなく日数で表示したい(2Months 2Daysなど) 「 62Days のように実際の日数を表示するだけにします。コードを変更しますか?ありがとう。

これが私がカウントダウンに使用しているコードです。

#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

次の2つのメソッドをクラスにコピーしてから、viewDidLoadメソッドでstartタイマーを呼び出します。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

以前は、ラベル(この場合はdateLabel)に値を指定していました。年、月、日、またはそれが何であれ、文字列を連結する必要があります...

于 2012-11-20T12:36:42.093 に答える
0

2つの日付から日を取得するには、このメソッドを試してください。

- (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 に答える