संभव डुप्लिकेट:
iOS and finding Tomorrowमैं NSDate का उपयोग कर अगली तारीख कैसे प्राप्त कर सकता हूं?
मैं NSDate का उपयोग कर अगले स्थिति कैसे प्राप्त कर सकते हैं। कृपया मुझे समाधान भेजें।
संभव डुप्लिकेट:
iOS and finding Tomorrowमैं NSDate का उपयोग कर अगली तारीख कैसे प्राप्त कर सकता हूं?
मैं NSDate का उपयोग कर अगले स्थिति कैसे प्राप्त कर सकते हैं। कृपया मुझे समाधान भेजें।
निम्नलिखित में, आपकाडेट आपके इनपुट NSDate का प्रतिनिधित्व करता है; अगले दिन अगले दिन का प्रतिनिधित्व करता है।
// start by retrieving day, weekday, month and year components for yourDate
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
NSInteger theDay = [todayComponents day];
NSInteger theMonth = [todayComponents month];
NSInteger theYear = [todayComponents year];
// now build a NSDate object for yourDate using these components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:theDay];
[components setMonth:theMonth];
[components setYear:theYear];
NSDate *thisDate = [gregorian dateFromComponents:components];
[components release];
// now build a NSDate object for the next day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
[offsetComponents release];
[gregorian release];
हाय, हाँ इस कोड का काम कर धन्यवाद एक बहुत, आप कैसे पिछले दिन (कल) प्राप्त करने के लिए के बारे में कोई विचार है? – Jai
बदलना [ऑफसेट कॉम्पोनेंट्स सेटडे: 1] सेट करने के लिए: -1 परिणाम – rpetrich
जोड़ने के बजाय एक दिन घटाना परिणाम यह अगली तारीख डेलाइट सेविंग मानक समय तक चलता है जब यह भी काम करता है। 2011-11-06 से आगे बढ़ने का प्रयास करें। धन्यवाद @unforgiven। – johnnieb
// विधि या viewDidLoad
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"Date"];
// इस कोड को जहां अगले या पिछले दिनांकों
NSDate *tomorrow = [[NSUserDefaults standardUserDefaults] valueForKey:@"Date"];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *date = [tomorrow addTimeInterval:secondsPerDay]; //Change NSDate *date = [tomorrow addTimeInterval:-secondsPerDay]; for yesterday
tomorrow = date;
[self setDate:tomorrow];
dateTextField.text = [self getDate];
[[NSUserDefaults standardUserDefaults] setObject:tomorrow forKey:@"Date"];
गुफा: बार-बार बचत समय को देखते हुए एक एनएसडीएटी में addTimeInterval के साथ सेकंड्सपेरडे (24 * 60 * 60) सेकेंड जोड़ना किया जाता है। दिन में 24 घंटे जोड़ते समय जब आप डेलाइट savin समय में प्रवेश करते हैं या छोड़ते हैं तो दिन के समय +/- 1 घंटे तक छोड़ देता है। –
इस तरीकों के बारे में क्या पुनः प्राप्त करना चाहते हैं जोड़े init करने के लिए नीचे दिए गए कोड जोड़ें
http://www.drobnik.com/touch/2009/05/adding-days-to-nsdate/
NSDate *now = [NSDate date];
int daysToAdd = 50; // or 60 :-)
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];
// create a calendar
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];
NSLog(@"Clean: %@", newDate2);
** इस विषय में सबसे ऊपर से ऊपर की तुलना में अधिक ** बेहतर समाधान –
मैं इस विधि मेरे लिए बस ठीक काम करता है लगता है।
NSString *zajtra = [[NSDate date] dateByAddingTimeInterval:86400];
NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];
बहुत अधिक सबसे आसान तरीका है ..
यह तिथियों की सीमा में डेलाइट सेविंग्स टाइम चेंज होने पर गलत परिणाम दे सकता है। – progrmr
इसके अलावा जब एक छलांग दूसरा होता है। –
मेरे लिए अच्छा विचार –
आप सही नीचे जवाब में से एक को चिह्नित यदि आपको लगता है कि यह आपके सवाल का जवाब चाहिए। –