Datepicker picks wrong date

前端 未结 4 993
北荒
北荒 2021-01-05 16:53

I am converting date to day, but if I just println() the current selected date in the datepicker, I get wrong time and wrong date.

@IBOutlet weak var datepic         


        
4条回答
  •  轮回少年
    2021-01-05 17:43

    This is fine because the 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:

    extension NSDate {
        var localizedDescription: String {
           return descriptionWithLocale(NSLocale.currentLocale()) ?? ""
        }
    }
    
    println(chosenDate.localizedDescription)
    

    Swift 3 or later

    extension Date {
        var localizedDescription: String {
            return description(with: .current)
        }
    }
    

    Date().localizedDescription  // "Monday, October 16, 2017 at 2:24:11 AM Brasilia Summer Time"
    

提交回复
热议问题