问题
I want add days to a date, I got many codes for this but none of them are working for me below shown is my code,please somebody help me to fix this issue
int daysToAdd=[[appDlegateObj.selectedSkuData
objectForKey:@"Release Time"] intValue];
NSTimeInterval secondsForFollowOn=daysToAdd*24*60*60;
NSString *dateStr=[contentDic objectForKey:@"Date"];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *dateFromString=[[NSDate alloc]init];
dateFromString=[dateFormatter dateFromString:dateStr];
[dateFormatter release];
NSDate *date=[dateFromString dateByAddingTimeInterval:secondsForFollowOn];
回答1:
// Initialize stringified date presentation
NSString *myStringDate = @"2011-11-17";
// How much day to add
int addDaysCount = 30;
// Creating and configuring date formatter instance
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
// Retrieve NSDate instance from stringified date presentation
NSDate *dateFromString = [dateFormatter dateFromString:myStringDate];
// Create and initialize date component instance
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:addDaysCount];
// Retrieve date with increased days count
NSDate *newDate = [[NSCalendar currentCalendar]
dateByAddingComponents:dateComponents
toDate:dateFromString options:0];
NSLog(@"Original date: %@", [dateFormatter stringFromDate:dateFromString]);
NSLog(@"New date: %@", [dateFormatter stringFromDate:newDate]);
// Clean up
[dateComponents release], dateComponents = nil;
[dateFormatter release], dateFormatter = nil;
Output:
Original date: 2011-11-17
New date: 2011-12-17
回答2:
NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
回答3:
With iOS 8 and 10.9, you can now actually do this a lot easier. You can add any number of any calendar unit to a date. I declared a really small category method on NSDate to make this even faster (since I just needed adding days throughout my app). Code below.
-(NSDate *)addDaysToDate:(NSNumber *)numOfDaysToAdd {
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:numOfDaysToAdd.integerValue toDate:self options:nil];
return newDate;
}
Key things: NSCalendarUnitDay is what I used to let it know I want to add days. You can change this to the other enum values like months or years.
Since it's a category on NSDate, I'm adding the value to self. If you don't want to declare a category, you'd just take a date in the method parameters, like:
-(NSDate *)addDays:(NSNumber *)numOfDaysToAdd toDate:(NSDate *)originalDate {
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:numOfDaysToAdd.integerValue toDate:originalDate options:nil];
return newDate;
}
I use a NSNumber, but you can easily just use an NSInteger.
Finally, in Swift:
func addDaysToDate(daysToAdd: Int, originalDate: NSDate) -> NSDate? {
let newDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.DayCalendarUnit, value: daysToAdd, toDate: originalDate, options: nil)
return newDate
}
If you want to get rid of the NSDate? and feel comfortable with unwrapping a possible nil (the date adding might fail), you can just return newDate! and get rid of the ? mark.
回答4:
You can add days to an NSDate by adding a time interval
NSDate* newDate = [date dateWithTimeInterval:3600*24*numberOfDays sinceDate:otherDate];
Although if you want to be really accurate about it, and take into account leap seconds, day light saving times and all this kind of thing you might want to use NSDateComponents as described in the Date and Time Programming Guide and Omtara's link.
回答5:
NSDateComponents *offsetComponents = [[NSDateComponents alloc] autorelease];
[offsetComponents setDay:1];
newDate = [[[CalendarContainer sharedCalendarContainer] gregorian] dateByAddingComponents:offsetComponents toDate:currentlyDisplayedDate options:0];
回答6:
I suggest you review this article for a cleaner solution.
回答7:
in Swift 2.1.X and xcode 7.1 OSX 10.10.5 ,you can add any number of days forward and backwards using function
func addDaystoGivenDate(baseDate:NSDate,NumberOfDaysToAdd:Int)->NSDate
{
let dateComponents = NSDateComponents()
let CurrentCalendar = NSCalendar.currentCalendar()
let CalendarOption = NSCalendarOptions()
dateComponents.day = NumberOfDaysToAdd
let newDate = CurrentCalendar.dateByAddingComponents(dateComponents, toDate: baseDate, options: CalendarOption)
return newDate!
}
function call for incrementing current date by 9 days
var newDate = addDaystoGivenDate(NSDate(), NumberOfDaysToAdd: 9)
print(newDate)
function call for decrement current date by 80 days
newDate = addDaystoGivenDate(NSDate(), NumberOfDaysToAdd: -80)
print(newDate)
回答8:
In Swift 2, use extension
to extend NSDate:
extension NSDate {
func addDays(days:Int) -> NSDate{
let newDate = NSCalendar.currentCalendar().dateByAddingUnit(
.Day,
value: days,
toDate: self,
options: NSCalendarOptions(rawValue: 0))
return newDate!
}
}
回答9:
NSDate *Date=[datePicker date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MM.yy EEEE h:mm a"];
来源:https://stackoverflow.com/questions/8164994/adding-days-to-nsdate