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
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)
NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
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.
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!
}
}
I suggest you review this article for a cleaner solution.
// 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