Search code examples
iosswiftdatejtapplecalendar

Set time (Hours, minutes) on Date object


I'm trying to let the user pick a date from the calendar and then set what time they want to be reminded on that date.

I get the date from this code:

func handleCellSelected(view: JTAppleCell?, cellState: CellState){
    guard let validCell = view as? CalendarCell else {return}
    if validCell.isSelected{
        validCell.selectedView.isHidden = false
        if userPickedDate {
            self.formatter.dateFormat = "YYYY MMMM dd, HH:mm"

            dateSelected.text = formatter.string(from: cellState.date)

This produces an output of the selected date with 00:00 in time. I want the user to be able to change the hours and minutes somehow, probably with a picker, to later be reminded on that date and time. Any ideas on how to change the hours and minutes inside the cellState.date?


Solution

  • You can set the datepicker's date to the cellState's date, and then you can only allow the datepicker to select hours and minutes. The resulting date from the datepicker will be the selected time on the date that you initially set it to. You can then grab that date and save it however you'd like.