0
我有一個的UIDatePicker,我用這個獲取日期:格式的UIDatePicker另一個NSDate的
NSDate *pickerDate = [datePickerView date];
我想創造一種是基於星期和時間的天將來的日期(HH :mm:ss)從pickerDate。下面是我使用的代碼,但它生成的日期是錯誤的:
更新的代碼
//format the uidatepicker
NSDate *dateSet = pickerDate;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@"EEEE HH:mm:ss"];
NSString *dateSetString = [df stringFromDate:dateSet];
NSLog(@"repeatDateString %@",dateSetString);//prints "Tuesday 12:12:43"
//create a new nsdate using the new format
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat2 setDateFormat: @"EEEE HH:mm:ss"];
NSDate *newDate = [[NSDate alloc] init];
newDate = [dateFormat2 dateFromString:dateSetString];
NSLog(@"new Date %@",newDate);//prints "1970-01-06 04:42:43 +0000"
我明白了。其實我設置了一個日期,我將使用我的localnotification的repeatInterval和Im使用NSWeekCalendarUnit。所以我想在特定的日期和時間使用日期重複我的通知,這就是爲什麼我想要創建一個nsdate,就像我在代碼中一樣。有沒有更好的方法呢? – Diffy 2012-04-03 04:13:55
那麼你最終想要結束的是「下一個星期二12:12:43」(從現在開始)? – lnafziger 2012-04-03 04:19:04
是的。這就是我想要的。 – Diffy 2012-04-03 04:25:45