如何在swift中从“2015-06-26T00:10:00 + 01:00”得到yyyy-mm-dd hh:mm:ss格式

我有卡在问题中,我必须转换date格式是2015-06-26T00:10:00 + 01:00成yyyy-mm-dd hh:mm:ss请给我一些想法如何做到这一点?

let formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd hh:mm:ss" let startTimeString = "2015-06-26T00:10:00+01:00" let startTime = formatter.dateFromString(startTimeString) print("start time : \(startTime)") 

输出是开始时间:无

 let startTimeString = "2015-06-26T00:10:00+01:00" // First convert the original formatted date/time to a string: let deFormatter = NSDateFormatter() deFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" let startTime = deFormatter.dateFromString(startTimeString) print(startTime!) // 2015-06-25 23:10:00 +0000 // Note that `println` use the `description` method which defaults to UTC. // Then convert the date/time to the desired formatted string: let formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let timeString = formatter.stringFromDate(startTime!) print(timeString) // 2015-06-25 19:10:00 // Note that `NSDateFormatter` defaults to the local time zone. 

请参阅: ICU格式化date和时间 )

你也试过这个吗?

 var formatter: NSDateFormatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss" let dateTimePrefix: String = formatter.stringFromDate(NSDate()) 

请试试这个:

 let dateString = "2015-06-26T00:10:00+01:00" let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxxxx" if let dateFromString = formatter.date(from: dateString) { formatter.dateFormat = "yyyy-MM-dd HH:mm" //which format is needed let stringFromDate = formatter.string(from: dateFromString) print(stringFromDate) }