I am using a datePicker
and trying to establish the minimum date as the current date and the maximum being a week from today. I cannot seem to get the max working. Here is my code:
calendarView.minimumDate = todaysDate
calendarView.maximumDate = [todaysDate .dateByAddingTimeInterval(60*60*24*10)]
Add Days to Current Date To add more days to a current date object, you can use the Calendar and the DateComponents() Swift structs. print(futureDate!) let currentDate = Date() var dateComponent = DateComponents() dateComponent. day = 1 let futureDate = Calendar.
To get the ISO week number (1-53), use calendar. component(. weekOfYear, from: date ) . To get the corresponding four-digit year (e.g. 2022), use calendar.
Use NSDate and NSDateFormatter. NSDate *today = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"dd/MM/yyyy"]; NSString *dateString = [dateFormat stringFromDate:today]; NSLog(@"date: %@", dateString);
You can do:
let today = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
let nextWeek = cal!.dateByAddingUnit(NSCalendar.Unit.Day, value: 7, toDate: today, options: NSCalendar.Options.MatchLast)
This will print out 2016-09-14 04:21:14
let calendar = Calendar.current
let addOneWeekToCurrentDate = calendar.date(byAdding: .weekOfYear, value: 1, to: Date())
This code works well even if over the year-end.
Calendar.current.date(byAdding: .weekOfYear, value: 1, to: date)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With