UIDatePicker Time Issues - ios

I have a problem with my UIDatePicker:
When it first appears on the view as you'll see in the screenshot,
the initial value of the UIDatePicker says Today 3:00 pm, but if I click on the done button the label shows 3:07 ( which is the actual time ).
I want the Label to display the same time as the UIDatePicker.
here is my code:
override func viewDidLoad() {
super.viewDidLoad()
myDatePicker.minuteInterval = 15
}
#IBAction func datePickerAction(sender: AnyObject) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
var strDate = dateFormatter.stringFromDate(myDatePicker.date)
self.selectedDate.text = strDate
}

You need to adjust the initial date to the discrete minute amounts at first. I am sure your date picker works just fine once you change the value, right? When you set the date picker to a date (like 3:07) but the date picker is set to show only increments of 15 minutes, it will show 3:00, but the set date is still 3:07.
let allUnits = NSCalendarUnit(rawValue: UInt.max)
let components = NSCalendar.currentCalendar().components(allUnits, fromDate: NSDate())
var minute = components.minute
minute = (minute / 15) * 15
components.minute = minute
let datePickerDate = NSCalendar.currentCalendar().dateFromComponents(components)
This always rounds the minutes downwards (while you might want to round up if the minute % 15 > 7 and then account for rounding up to 60), but you get the idea.

Related

how to set date and time limitation in Date Time Picker from Now to 1 Month from Now?

I now that there is a thread that discussing about minimum and maximum date time in here Minimum and maximum date in UIDatePicker
but I am a beginner now in programming, and I have little bit confused with date and time object. I have tried but I can't set up my date time picker properly. So I want to set my date time picker to have minimum and maximum limited time.
the minimum is Now, and the maximum is 1 month from now. what code do I have to use? Thanks in advance
https://i.stack.imgur.com/RxXUX.png
You can try
self.datePicker.minimumDate = Date()
self.datePicker.maximumDate = Calendar.current.date(byAdding: .day, value: 30 , to:Date())!
or
self.datePicker.maximumDate = Calendar.current.date(byAdding: .month, value: 1 , to:Date())!
I recommend to use the date math skills of Calendar. It calculates the same day in the next month. If the day does not exist (for example Feb 30) it returns the next closest available date.
let now = Date()
let calendar = Calendar.current
let dayComponents = calendar.dateComponents([.day], from: now)
let maximumDate = calendar.nextDate(after: now, matching: dayComponents, matchingPolicy: .nextTime)
To get a Date exactly one month in the future from now (taking into account different month lengths), you can use Date.date(byAdding:value:to:), e.g. as in this answer. You'd take the result and set it as maximumDate of your picker.

Set minimum and maximum date AND TIME in UIDatePicker in Swift

I have to set min and max dates AND time in date picker which I achieved with following properties:
datePicker.datePickerMode = UIDatePickerMode.dateAndTime
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = self.dateFormat
let minDate = dateFormatter.date(from: self.fromDate) //Day before yesterday
let maxDate = dateFormatter.date(from: self.toDate) //Yesterday
datePicker.minimumDate = minDate
datePicker.maximumDate = maxDate
However, I am also showing Time Picker with this, but it is limiting to only 00:00 value after changing time or date.
How do I set min time as 00:00 for 24-hrs time and 12:00 am for 12-hrs time? (Same for max time as 23:59 and 11:59 pm)

UIDatePicker minimal value? [duplicate]

This question already has answers here:
How do you create a Swift Date object?
(11 answers)
Closed 5 years ago.
In Swift, I have a var datePicker = UIDatePicker(). I'm trying to set the datePicker to have a minimum value of Jan, 1st, 1920 (I want a range from Jan, 1st, 1920 to current date. I read how to set the maximumDate of the UIDatePicker, that was easy, but I couldn't find how to set the minimum to a specific date. I read that it should be datePicker.minimumDate but I'm not sure how to set the exact date from above. Suggestions?
Create appropriate date components
let components = DateComponents(year: 1920, month: 1, day: 1)
Create a date from the components
let minimumDate = Calendar.current.date(from: components)
Set the minimum date
let picker = UIDatePicker()
picker.minimumDate = minimumDate
You can use the NSDateFormatter as the following:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DD"
let date = dateFormatter.dateFromString("1920-01-01")
let datePicker = UIDatePicker()
datePicker.minimumDate = date

How could I set UIDatePicker to show a day later from current date in swift?

I am trying to make an somewhat alarm app which has mandatory settings at first but the user can configure it by using the UIDatePicker.
The alarm app first has a defalut setting such as a week later from the current time, but it can be changed as the user scrolls the UIDatePicker. I searched a lot but cannot find a suitable answer.
can somebody please help me!
You can set the UIDatePicker date to your desired date time.
let numberOfDays = 6
datePicker.date = NSDate(timeIntervalSinceNow: (((24 * 60) * 60) * numberOfDays))
Hope it will help you..
Swift 4.1:
datePicker.date = Date(timeIntervalSinceNow: (((24 * 60) * 60) * numberOfDays))
Add this line of code when you create date picker.
datePicker.maximumDate = Calendar.current.date(byAdding: .day, value: 0, to: Date())
You can set value increase or decrease and complete your requirement.
let dateString = "Thursday, 19 Jan 2017"
let df = DateFormatter()
df.dateFormat = "eeee, dd MM yyyy"
let date = df.date(from: dateString)
if let unwrappedDate = date {
myDatePicker.setDate(unwrappedDate, animated: false) //set your own date
}

Slider with real time in Label (but setting the slider with time from label)

Just implemented this post :
Slider with real time in Label
With Denis code and works fine. Thanks.
Just translated to Swift, if anybody want:
func timeSlider(aSlider: UISlider)-> String {
let numberOfSlots = 24*4 - 1
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "h-mm"
let zeroDate = dateFormat.dateFromString("00-00")
let actualSlot = roundf(Float(numberOfSlots)*Float(aSlider.value))
let slotInterval:NSTimeInterval = NSTimeInterval(actualSlot * 15 * 60)
let slotDate: NSDate = NSDate(timeInterval: slotInterval, sinceDate: zeroDate!)
dateFormat.dateFormat = "h-mm"
return dateFormat.stringFromDate(slotDate)
}
Now I'm trying to figure out how make the inverse operation. I mean, in my database I'll store the time, shown on label.
When I query the database, how can I set the slider ?
My slider is showing values like:
2015-08-21 20:46:14.831 [5306:1205420] 0.303571
2015-08-21 20:46:14.847 [5306:1205420] 0.292411
2015-08-21 20:46:14.864 [5306:1205420] 0.276786
While my slider has minumum = 0 and maximum = 1
Any help ?
Thank you
UISlider has a method - setValue:animated: that you can use to move the slider
Here is the documentation
----EDIT----
In order to convert the time value to a value appliable to the slider you need to do the following:
(dbTime - minTime) / (maxTime - minTime)
as long as the dbTime is inbetween the min and max value, it will give a value between 0 and 1

Resources