2011-12-29 4 views
0

こんにちは私はiOSプログラミングの新機能で、日付(今日の日付と1年後の日付)で遊んでいます。後で使用できるように日付を保存しようと苦労しています

ここでは、私がやっているコードです。

NSCalendar * calendar = [NSCalendar currentCalendar];//Create a calendar 
NSDate *todaysDate = [[NSDate alloc]init]; //get todays date 
NSString *dateToday = [NSString stringWithFormat:@"%@",todaysDate];//convert it to a string 
NSDateFormatter *df = [[NSDateFormatter alloc] init];// create a formatter 
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ" ];//input how the date looks as a string 
myDate = [df dateFromString: dateToday];// change it back to a proper NSDate 

NSDateComponents *components = [[NSDateComponents alloc] init]; // create the components 
[components setYear:1];//add 1 year 
nextYear = [calendar dateByAddingComponents:components toDate:todaysDate options:0]; // build the year from component into a variable 


dateNextYear = [NSString stringWithFormat:@"%@",nextYear];//convert it to a string 
NSDateFormatter *yearFormat = [[NSDateFormatter alloc] init];// create a formatter 
[yearFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ" ];//input how the date looks as a string 
myDateInTheFuture = [yearFormat dateFromString: dateNextYear];// change it back to a proper NSDate 


NSLog(@" next years date is %@ ", myDateInTheFuture); 
[yearFormat release]; 
[components release]; 
[todaysDate release]; 

私は今から、現在の日付と将来の日付1年を得ることができますが、私は比較のために、将来の日付を保存する方法をわからないよ、私は私がにNSDateのための「比較」アイテムを使用することができます知っていますチェックしますが、将来の日付を変数に設定するときは毎回実行されますが、それは今日の日付との比較から1年間離れています。 その3am私はどこに私の脳が茂っているので、これはこれまでの最も単純なことであり、私はそれを見ることができない場合は事前に謝罪する。

私の最初の投稿ので、私にしてください。 おかげ

答えて

1

私は何をしようとする全くわからないが、これは私が集まるものです:

あなたは、現在の日付を取ることに年を追加し、結果の日付を操作したいです。

これが間違っている場合はお知らせください。このため

、以下を試してみてください。

NSDate *todayDate = [NSDate date]; //Create a date that is set to today 
NSDate *resultingDate = [calendar dateByAddingTimeInterval:31556926; //Take the current date and add the amount of seconds in a year to it 

あなたは永久にこれを保存したい場合は、NSUserDefaultsを使用します。設定する

を:

[userDefaults setObject:resultingDate forKey:@"storedDate"]; 

希望これは、

を支援

HBhargava

取得する

:でなければならない特殊な例負荷があるよう

NSDate *returnedDate = [userDefaults dateForKey:@"storedDate"]; 
+0

を日/週/年の/ etcのN番号で日付を調整するためにハードコードされた時間間隔を使用するには、恐ろしいアイデア(TM)でありますを占めた。事例:サモアは今年12月30日を削除しています。 http://www.bbc.co.uk/news/business-13330592 日付APIを使用して、カレンダー計算を実行し、将来的に多くの髪を引っ張らないようにします。 – MyztikJenz

+0

ありがとうhbhargava私が探していた答えはユーザーのデフォルトルートでした。私はあなたが私が何かをしたいものを複雑にしているように見えるように時間を割いていただきありがとうございます。乾杯 – pidge

関連する問題