How to convert date (without knowing the kind) to string

无人久伴 提交于 2019-12-04 07:44:05

Example using NSDataDetector (Swift 2.0):

let textDate = "dan's march 31, 1975 12:34 pm";

do {
    let dd = try NSDataDetector(types: NSTextCheckingType.Date.rawValue)
    if let tcr :NSTextCheckingResult = dd.firstMatchInString(textDate, options:[], range:NSMakeRange(0, textDate.utf16.count)),
       let date = tcr.date {
            print("date: \(date)")
        }
}
catch {
    print("Data Detector error!")
}

Output:

date: 1975-03-31 16:34:00 +0000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!