代码之家  ›  专栏  ›  技术社区  ›  Ben

将传递的Unix NSDate转换为字符串Swift 4[重复]

  •  0
  • Ben  · 技术社区  · 6 年前

    我试图将传递的NSDate转换为UILabel的可读日期/时间字符串。由于某些原因,标签在输出中显示了意外的日期。

    struct myDateRange{
        var startDate: NSNumber?
        var endDate: NSNumber?
    }
    
    func configureDate(date:myDateRange){
    
        let date = NSDate(timeIntervalSince1970: date.startDate as! TimeInterval)
        let timeFormat = DateFormatter()
        timeFormat.timeStyle = .short
        timeFormat.dateStyle = .short
        let currentTime = "\(timeFormat.string(from: date as Date))"
        timeLabel.text = String(currentTime)
    }
    

    输出:

    通过:可选(1518393600000)

    labeltext:12/23/85,上午10:30

    预期:

    02/12/18, 12:00

    0 回复  |  直到 6 年前