action listener for timePicker

2019-08-19 08:10发布

问题:

I have a timePicker which I have declared a listener if user change the time string sets on textFiled.

func timeFromAction(){

    DispatchQueue.main.async {

        self.timePickerFrom.datePickerMode = UIDatePickerMode.time
        //self.timePickerFrom.minimumDate = self.minDate
        print(self.minDate)
        self.timePickerFrom.date = self.minDate // current date I have achieve from millisecond to date

        self.timePickerFrom.locale = Locale(identifier: "fa_IR")
        self.timePickerFrom.calendar = Calendar(identifier: Calendar.Identifier.persian)

        self.timePickerFrom.timeZone = TimeZone(abbreviation: "IRST")

        self.timePickerFrom.frame=CGRect(x: 0, y: 0, width: 288, height: 83)

        self.timeOfLoading.addSubview(self.timePickerFrom)

        self.timePickerFrom.addTarget(self, action: #selector(self.dateChangedFrom(_:)), for: .valueChanged)

    }
}

this is the listener function :

func dateChangedFrom(_ sender: Any) {

    let date = self.timePickerFrom.date
    print(date)
    let components = Calendar.current.dateComponents([.hour, .minute], from: date)
    let hour = components.hour!
    let minute = components.minute!
    self.timeOfloadingLable.text = "\(hour):\(minute)"

}

so the problem is when I change the time no matter hour or minute.time goes 1 hour forward.

in listener function I'm getting date from timePicker which when I print it it is wrong although I have set a correct date on it

update :

self.minDate=self.crrnt.getComperehensiveDate()

and this is the function I use for it :

func setComperehensiveDate(longDate:Double){

        let dbl = TimeInterval(longDate)

        let date = Date(timeIntervalSince1970: dbl / 1000)


        resultDate = date

    }

 func getComperehensiveDate()->Date{

        print("bbbbbb",resultDate!)
        return resultDate!
    }

when time is 11:59 shows me 11:33 like below

cheek this video : link

回答1:

I just checked your code, It's working well, Please make sure your longDate is correct.

You're passing wrong epoch time. Check your epoch time here: https://www.epochconverter.com/