Date Picker, User cannot go back to previous date Swift - swift

I have Date Picker
I already set the current Date, and i want to make User cannot scroll back to previous date? is it allowed?

As the documentation says here
you have just to set the option minimumDate of your UIDatePicker.
This is an example
var datePicker = UIDatePicker() //your UIDatePicker
datePicker.minimumDate = Date() //the minimum date is now. Only future date is allowed
Another case is the following
var datePicker = UIDatePicker() //your UIDatePicker
//create a gregorian calendar
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
// set the timezone
calendar.timeZone = NSTimeZone(name: "UTC")!
// create and set the component (for your minimum date value)
let components: NSDateComponents = NSDateComponents()
components.calendar = calendar
components.year = -100
// obtain the minimum NSDate, according to the defined components
//it will result as the difference of the years (components.year) from now (NSDate())
let minDate100YearsAgo: NSDate = calendar.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))!
//then you add this to the UIDatePicker
datePicker.minimumDate = minDate100YearsAgo

This is an example to how to do it in Swift 2.2:
// a global var
var datePicker = UIDatePicker()
// UIDatePicker settings:
let currentDate: NSDate = NSDate()
let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
calendar.timeZone = NSTimeZone(name: "UTC")!
let components: NSDateComponents = NSDateComponents()
components.calendar = calendar
components.year = -18
let minDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
components.year = -150
let maxDate: NSDate = calendar.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
datePicker.dateOfBirthUIDatePicker.minimumDate = minDate
datePicker.dateOfBirthUIDatePicker.maximumDate = maxDate

At Swift 3:
var datePicker = UIDatePicker()
datePicker.minimumDate = Date()

Related

DatePicker returning Wrong Date Swift 5

Programmatically implemented
set limit
let calendar = Calendar.current
let currentDate = Date()
var components = DateComponents()
components.calendar = calendar
components.year = -100
let minDate = calendar.date(byAdding: components, to: currentDate)!
components.year = -7
let maxDate = calendar.date(byAdding: components, to: currentDate)!
datePicker.minimumDate = minDate
datePicker.maximumDate = maxDate
method call on value change
#objc fileprivate func datePickerChanged() {
print(datePicker.date)
}
Target
datePicker.addTarget(self, action: #selector(datePickerChanged), for: .valueChanged)
You can fix this issue by adding the following
datePicker.timeZone = TimeZone(secondsFromGMT: 0)

How do i set the datepicker that allows user to choose between the current date and the current date + 15 days?

This is my current code. I am not too sure how minimum and maximum date works. I have look some of the post regarding this matter but still could not get the code to work.
let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
var components = DateComponents()
components.calendar = calendar
components.day = 15
let maxDate = calendar.date(byAdding: components, to: currentDate)!
picker.minimumDate = currentDate
picker.maximumDate = maxDate

How to fetch day, month and hour from NSDate in Swift

I have an NSDate object called reservationDate and I want to fetch day, month and hour from the Date. However, I'm getting wrong values. How can I get these values?
The value of the reservationDate object
reservationDate NSDate 2016-08-05 21:00:00 UTC
thus I expect to get 5 as a day and 21 as a hour however I'm receiving 6 as a day and 0 as a hour. Should I expect something else?
let unitFlags: NSCalendarUnit = [.Hour, .Day, .Month, .Year]
let components = NSCalendar.currentCalendar().components(unitFlags, fromDate: reservationDate)
let day = components.day
let hour = components.hour
You have not set Time zone. Please assign it "UTC".
NSCalendar.currentCalendar().timeZone = NSTimeZone(name: "UTC")!
Try to set timezone with Calendar object with UTC
let unitFlags: NSCalendarUnit = [.Hour, .Day, .Month, .Year]
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone(name: "UTC")!
let components = calendar.components(unitFlags, fromDate: NSDate())
let day = components.day
let hour = components.hour
let currentDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
var convertedDate = dateFormatter.stringFromDate(currentDate)
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy, HH:mm"
convertedDate = dateFormatter.stringFromDate(currentDate)

How can i divide NSDate() into pieces?

func date() -> String {
return NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.MediumStyle)
}
var date = date() // 2016. 5. 13. 오전 4:45:16
above code, date obtain korean current value of date every time that i call date()
I'll hope to have refresh new value from NSDate(), and so insert refresh new value into varibles like below code
var yearMonDay = "2016. 5. 13"
var hourMinSec = "오전 4:45:16"
hmmm Are there methods to divide NSDate() into pieces like below code?
yearMonDay = NSDate().?? // refresh date "year: month: day"
hourMinSec = NSDate().?? // refresh date "am/fm hour:minute:second
Splitting up components like hour can be done using the components of the NSCalendar
let today = NSDate()
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: today)
print("\(components.month) \(components.day) \(components.year)")
print("\(components.hour) \(components.minute) \(components.second)")
With the NSDate formatter you can find the names
let formatter = NSDateFormatter()
let monthName = formatter.monthSymbols[components.month-1]
print(monthName)
You can use the NSDateFormatterStyle:
// get the current date and time
let currentDateTime = NSDate()
// initialize the date formatter and set the style
let formatter = NSDateFormatter()
// October 26, 2015
formatter.timeStyle = NSDateFormatterStyle.NoStyle
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.stringFromDate(currentDateTime)
// 6:00:50 PM
formatter.timeStyle = NSDateFormatterStyle.MediumStyle
formatter.dateStyle = NSDateFormatterStyle.NoStyle
formatter.stringFromDate(currentDateTime)

Creating a future date in swift with NSDate()

I'm getting an error: "Missing argument for parameter 'coder' in call" for the following code:
var components = NSDateComponents()
components.setValue(1,forComponent: NSCalendarUnit.CalendarUnitMonth);
var expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(),options:0)
The docs calls for:
NSCalendar.currentCalendar().dateByAddingComponents(<#comps: NSDateComponents?#>, toDate: <#NSDate?#>, options: <#NSCalendarOptions#>)
Can anyone see what I'm missing? I don't see a parameter named 'coder' required.
how it looks to be with
swift 4.x
let date = Date()
var components = DateComponents()
components.setValue(1, for: .month)
let expirationDate = Calendar.current.date(byAdding: components, to: date)
swift 2.0
let components: NSDateComponents = NSDateComponents()
components.setValue(1, forComponent: NSCalendarUnit.Month);
let date: NSDate = NSDate()
let expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: NSCalendarOptions(rawValue: 0))
swift 1.2
var components = NSDateComponents()
components.setValue(1, forComponent: NSCalendarUnit.CalendarUnitMonth);
let date: NSDate = NSDate()
var expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: date, options: NSCalendarOptions(0))
Objective-C
NSDate *date = [NSDate new];
NSDateComponents *components = [NSDateComponents new];
components.month = 1;
NSDate *expirationDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:date options:0];
Syntax for Swift 3
var components = DateComponents()
components.setValue(1, forComponent: .month)
let date: Date = Date()
let expirationDate = Calendar.current.date(byAdding: components, to: date, options: [])
Latest Swift 3 syntax:
var components = DateComponents()
components.setValue(1, for: .month)
let date: Date = Date()
let expirationDate = Calendar.current.date(byAdding: components, to: date)
Swift 4 version
var components = DateComponents()
components.setValue(1, for: .month)
let date: Date = Date()
let expirationDate = Calendar.current.date(byAdding: components, to: date, wrappingComponents: false)
Hope this helps!