UIDatePicker显示错误的时间swift

时间:2021-01-30 18:09:01
func show(title: String, doneButtonTitle: String = "Done", cancelButtonTitle: String = "Cancel", defaultDate: NSDate = NSDate(), datePickerMode: UIDatePickerMode = .DateAndTime, callback: DatePickerCallback) {
    self.titleLabel.text = title
    self.doneButton.setTitle(doneButtonTitle, forState: .Normal)
    self.cancelButton.setTitle(cancelButtonTitle, forState: .Normal)
    self.datePickerMode = datePickerMode
    self.callback = callback
    self.defaultDate = defaultDate
    self.datePicker.datePickerMode = self.datePickerMode ?? .Date
    self.datePicker.date = self.defaultDate ?? NSDate()
    self.datePicker.locale = NSLocale.currentLocale()
    /* */
    UIApplication.sharedApplication().windows.first!.addSubview(self)
    UIApplication.sharedApplication().windows.first!.endEditing(true)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationDidChange:", name: UIDeviceOrientationDidChangeNotification, object: nil)

    /* Anim */
    UIView.animateWithDuration(
        0.2,
        delay: 0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations: { () -> Void in
            self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
            self.dialogView!.layer.opacity = 1
            self.dialogView!.layer.transform = CATransform3DMakeScale(1, 1, 1)
        },
        completion: nil
    )
}

Hi , when i call self.datePicker.date return 3 hours back. How can i fix this problem i added self.datePicker.locale = NSLocale.currentLocale() but the problem not fixed. I can add more information if needed.

嗨,当我打电话给self.datePicker.date回来3个小时。我怎么能解决这个问题我添加了self.datePicker.locale = NSLocale.currentLocale()但问题没有修复。如果需要,我可以添加更多信息。

1 个解决方案

#1


0  

date picker uses its local time but it stores UTC time. It means your local time offset is -5h. You can do as follow to extract the right date and time from it:

日期选择器使用其本地时间但存储UTC时间。这意味着您当地的时间偏差为-5小时。您可以按照以下步骤从中提取正确的日期和时间:

extension NSDate {
    var localTime: String {
       return descriptionWithLocale(NSLocale.currentLocale())! 
    }
}

println(chosenDate.localTime)

Props to Leo`

道琼斯的道具

#1


0  

date picker uses its local time but it stores UTC time. It means your local time offset is -5h. You can do as follow to extract the right date and time from it:

日期选择器使用其本地时间但存储UTC时间。这意味着您当地的时间偏差为-5小时。您可以按照以下步骤从中提取正确的日期和时间:

extension NSDate {
    var localTime: String {
       return descriptionWithLocale(NSLocale.currentLocale())! 
    }
}

println(chosenDate.localTime)

Props to Leo`

道琼斯的道具