Trying to Set Only Time in Uidatepicker in Swift 2.0

Trying to set only time in UIDatePicker in Swift 2.0

Try setting its Mode:

starttimedatePicker.datePickerMode = UIDatePickerMode.Time

How to only display the time in UIDatePicker?

Set

datePicker.datePickerMode = UIDatePickerMode.Time

How to get the date from an UIDatePicker with mode set to Time only

You can get current date creating a Date object "Date()", and you could use the "valueChanged" event to merge both values, the time they picked and the date you got

Is it possible to set UIDatePicker to only days (exclude hours, minutes and seconds)?

you need to chose the mode of the date picker

@IBOutlet weak var myDatePicker: UIDatePicker!

override func viewDidLoad() {
super.viewDidLoad()
myDatePicker.datePickerMode = .date // or .Date (Swift 2.x)
}

date picker options .

Declaration SWIFT

var datePickerMode: UIDatePickerMode

Discussion The
value of this property indicates the mode of a date picker. It
determines whether the date picker allows selection of a date, a time,
both date and time, or a countdown time. The default mode is
UIDatePickerModeDateAndTime. See Date Picker Mode for a list of mode
constants.

UIDatePickerMode

The mode of the date picker.

Declaration Constants

Time

The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].

Available in iOS 2.0 and later.

Date

The date picker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example of this mode is [ November | 15 | 2007 ].

Available in iOS 2.0 and later.

DateAndTime

The date picker displays dates (as unified day of the week, month, and day of the month values) plus hours, minutes, and (optionally) an AM/PM designation. The exact order and format of these items depends on the locale set. An example of this mode is [ Wed Nov 15 | 6 | 53 | PM ].

Available in iOS 2.0 and later.

CountDownTimer

The date picker displays hour and minute values, for example [ 1 | 53 ]. The application must set a timer to fire at the proper interval and set the date picker as the seconds tick down.

Available in iOS 2.0 and later.

How to set Minimum time to appear as default in UIDatepicker everytime

EDIT:

int startHour = 7;
int endHour = 11;

NSDate *date1 = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date1];
[components setHour: startHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *startDate = [gregorian dateFromComponents: components];

[components setHour: endHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *endDate = [gregorian dateFromComponents: components];

[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];
[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];

How to make a time picker view in swift 3.0?

Set the datePickerMode to time

Swift 3

self.datePicker.datePickerMode = .time

Swift 2.3 or lower

self.datePicker.datePickerMode = .Time

Note: This will allow you to only select Hours and Minutes, if you want to select Seconds also you need to go for custom time picker.

Objective-C: UIDatePicker UIControlEventValueChanged only fired on second selection

This seems to be a bug in the iOS 7 implementation of UIDatePicker. I'd suggest filing a radar on it.

Building on the iOS 6 SDK and running on an iOS 6 device will work, while running on an iOS 7 device will not.

You can fix it by adding this line of code to your - (void)viewDidLoad method

[self.datePicker setDate:[NSDate date] animated:YES];

Minimum and maximum date in UIDatePicker

Try this:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-18];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-150];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];

self.datePicker.minimumDate = minDate;
self.datePicker.maximumDate = maxDate;

It may be easily translated to Swift 4.2:

    let calendar = Calendar(identifier: .gregorian)

let currentDate = Date()
var components = DateComponents()
components.calendar = calendar

components.year = -18
components.month = 12
let maxDate = calendar.date(byAdding: components, to: currentDate)!

components.year = -150
let minDate = calendar.date(byAdding: components, to: currentDate)!

picker.minimumDate = minDate
picker.maximumDate = maxDate

Swift: Get correct time zone from Date Picker?

You cannot "get a time zone" from a date picker. You can just get a date. The date will be independent on the current time zone of the device.

Perhaps you think you have a different date, but actually, there is no such thing as a "UTC date" or "EST date". Instead, there is only one date, and you use date formatters to display them for various time zones.

Note that there is quite a bit of redundancy in your code. The default locale and time zone of a date formatter are already the same values that you set. Also, when you have a method that returns a NSDate you do not have annotate the constant with : NSDate, making your code more verbose and cluttered.

Note that if you print a date the console will always show UTC. e.g.

let date = NSDate() // Nov 10, 9:44 PM
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd hh:mm a"
let dateString = dateFormatter.stringFromDate(date) // "2015-11-10 09:44 PM"
print(date) // "2015-11-10 20:44:54 +0000\n"


Related Topics



Leave a reply



Submit