这绝对不是最有效的方法,但相当简单,编码时很有趣:
var dates = ["11 Aug 2015",
"24 Sep 2014",
"25 Aug 2014",
"07 Aug 2014",
"07 Aug 2014"]
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var monthsNum = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
func convertToInt (date : String) -> Int {
var arr = split(date) {$0 == " "}
var c = 1
for month in months {
if (arr[1] == month) {
if (c < 10) {
return (arr[2]+"0"+String(c)+arr[0]).toInt()!
}
else {
return (arr[2]+String(c)+arr[0]).toInt()!
}
}
c++
}
return 0
}
func convertToString (date : Int) -> String {
var dateString = String(date) as NSString
var year = dateString.substringWithRange(NSRange(location: 0, length: 4))
var month = dateString.substringWithRange(NSRange(location: 4, length: 2))
var day = dateString.substringWithRange(NSRange(location: 6, length: 2))
var c = 0
for curMonth in monthsNum {
if (curMonth == month) {
return year+" "+months[c]+" "+day
}
c++
}
return ""
}
var newDates = [Int]()
for date in dates {
newDates.append(convertToInt(date))
}
println(convertToString(maxElement(newDates)))
println(convertToString(minElement(newDates)))
NSDate
对象,然后在其上运行最大/最小日期。 - Shamas S