日期格式化程序在正确的日期失败

时间:2017-11-01 12:18:12

标签: swift nsdate nsdateformatter

我有一个将String解析为Date的函数。该功能在除“26-3-2017”之外的所有日期都能正常工作。 如果我操纵日期并使其“26-3-2016”一切正常。 更重要的是,我在一系列日期上尝试了这个功能,除了“26-3-2017”之外,它在所有这些日期都能正常工作。

CODE

import Foundation
let dates = ["1-11-2017",
"17-10-2017",
"16-10-2017",
"13-10-2017",
"13-10-2017",
"10-10-2017",
"5-10-2017",
"4-10-2017",
"2-10-2017",
"26-9-2017",
"20-9-2017",
"18-9-2017",
"18-9-2017",
"27-5-2017",
"15-5-2017",
"9-5-2017",
"9-5-2017",
"5-5-2017",
"12-4-2017",
"26-3-2017",
"25-3-2017",
"22-3-2017",
"15-3-2017",
"10-3-2017",
"10-3-2017",
"17-2-2017",
"18-12-2016",
"17-12-2016",
"5-12-2016",
"6-12-2016",
"5-12-2016",
"6-12-2016",
"5-12-2016",
"6-12-2016",
"2-12-2016",
"30-11-2016",
"30-11-2016",
"30-11-2016",
"25-11-2016",
"25-11-2016",
"29-11-2016",
"23-11-2016",
"18-11-2016",
"18-11-2016",
"21-10-2016",
"21-10-2016",
"17-10-2016",
"17-10-2016",
"14-10-2016",
"12-10-2016",
"12-10-2016",
"17-12-2016",
"7-10-2016",
"23-7-2016",
"22-7-2016",
"22-7-2016",
"22-7-2016",
"21-7-2016"]

func getDate(dateString:String, withFormat format:String = "d-M-yyyy") -> Date?{
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat  = format

    return dateFormatter.date(from: dateString)!
}

for date in dates{

    print (getDate(dateString: date))
}

0 个答案:

没有答案