我想創建倒計時的天數直到某個日期。我對小時或分鐘不感興趣,只是從當前日期到設定日期的天數。這是我一直在嘗試:NSDate天數倒計時
NSDate *date = [NSDate date];
NSString *dateString = [date description];
// this **dateString** string will have **"yyyy-MM-dd HH:mm:ss +0530"**
// NSMutableString *mutString = dateString;
NSArray *arr = [dateString componentSeperatedByString:@" "];
// arr will have [0] -> yyyy-MM-dd, [1] -> HH:mm:ss, [2] -> +0530 (time zone)
但它不工作,因爲目前的日期是分鐘和秒的時間。
//NSDate *currentDate = [[NSDate alloc] init];
NSString *start = [arr objectAtIndex:0];//[NSString stringWithFormat:@"%@", currentDate];//@"2011-11-05";
NSLog(@"%@",start);
NSString *end = @"2011-6-03";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
[f release];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
fromDate:startDate
toDate:endDate
options:0];
NSInteger days = [components day];
[gregorianCalendar release];
NSLog(@"%i", days);
我真的不明白大部分代碼,我不familar用的NSDate和格式化,但是這是我的一幫網絡代碼的組合。
在此先感謝。
如何設置新日期 – Andrew 2011-04-20 01:34:34
完美。非常感謝! – Andrew 2011-04-20 01:41:20