こんにちは私は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私はどこに私の脳が茂っているので、これはこれまでの最も単純なことであり、私はそれを見ることができない場合は事前に謝罪する。
私の最初の投稿ので、私にしてください。 おかげ
を日/週/年の/ etcのN番号で日付を調整するためにハードコードされた時間間隔を使用するには、恐ろしいアイデア(TM)でありますを占めた。事例:サモアは今年12月30日を削除しています。 http://www.bbc.co.uk/news/business-13330592 日付APIを使用して、カレンダー計算を実行し、将来的に多くの髪を引っ張らないようにします。 – MyztikJenz
ありがとうhbhargava私が探していた答えはユーザーのデフォルトルートでした。私はあなたが私が何かをしたいものを複雑にしているように見えるように時間を割いていただきありがとうございます。乾杯 – pidge