Swift NsdateFormatter问题

时间:2015-12-17 04:48:44

标签: ios swift nsdateformatter

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'"
let dateShow = formatter.dateFromString(datestring[row]["time"]!)
let finalFormatter = NSDateFormatter()
finalFormatter.dateFormat = "HH:mm"
cell.textField?.stringValue = finalFormatter.stringFromDate(dateShow!)

我尝试将字符串更改为时间格式但崩溃

4 个答案:

答案 0 :(得分:0)

请你检查一下datetring [row] [" time"]返回的值!)。请查看下面使用硬编码值的示例。

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'"

let dateShow = formatter.dateFromString("2015-12-17T10:27:02")

let finalFormatter = NSDateFormatter()
finalFormatter.dateFormat = "HH:mm"
finalFormatter.stringFromDate(dateShow!)

答案 1 :(得分:0)

试试这个对我有用......

let formatter = NSDateFormatter()
 formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'"
let dateShow : NSDate = formatter.dateFromString(datestring[row]["time"]!)
 formatter.dateFormat = "HH:mm"
cell.textField?.text = finalFormatter.stringFromDate(dateShow!) as! String

答案 2 :(得分:0)

试试这个: -

 let formatter = NSDateFormatter()
 formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'"
 let dateShow = formatter.dateFromString("2015-12-19T08:20:21")
 let finalFormatter = NSDateFormatter()
 finalFormatter.dateFormat = "HH:mm"
 finalFormatter.stringFromDate(dateShow!)
 textField.text = finalFormatter.stringFromDate(dateShow!) as String

答案 3 :(得分:0)

            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let date = dateFormatter.dateFromString(TxnTableData[row]["time"]!)
            // To convert the date into an HH:mm format
            dateFormatter.dateFormat = "HH:mm"
            let dateString = dateFormatter.stringFromDate(date!)
            print("time: \(dateString)")

我使用此代码解决了这个问题我发布了这个答案因为将来它会帮助其他人