如何将这个日期时间从日期转换?

日期:2016-02-29 12:24:26 至2016年2月29日

到目前为止,这是我的代码,它返回一个nil值:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date: NSDate? = dateFormatter.dateFromString("2016-02-29 12:24:26")
print(date)

当前回答

把它放在扩展中,并像下面这样调用它。它很容易在整个应用程序中使用。

self.getFormattedDate(strDate: "20-March-2019", currentFomat: "dd-MMM-yyyy", expectedFromat: "yyyy-MM-dd")

实现

func getFormattedDate(strDate: String , currentFomat:String, expectedFromat: String) -> String{
    let dateFormatterGet = DateFormatter()
    dateFormatterGet.dateFormat = currentFomat

    let date : Date = dateFormatterGet.date(from: strDate) ?? Date()

    dateFormatterGet.dateFormat = expectedFromat
    return dateFormatterGet.string(from: date)
}

其他回答

在iOS 15中使用如下代码:

extension Date {
    var string: String {
        if #available(iOS 15.0, *) {
            return self.formatted(date: .complete, time: .complete)
        } else {
            return self.description
        }
    }
}

如果你想解析"1996-12-19T16:39:57-08:00"的日期,请使用以下格式"yyyy-MM-dd 'HH:mm:ssZZZZZ":

let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

/* 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time) */
let string = "1996-12-19T16:39:57-08:00"
let date = RFC3339DateFormatter.date(from: string)

来自Apple https://developer.apple.com/documentation/foundation/dateformatter

对于Swift 4.2, 5

以任何你想要的方式传递日期和格式。 要选择格式,您可以访问NSDATEFORMATTER网站:

static func dateFormatter(date: Date,dateFormat:String) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = dateFormat
    return dateFormatter.string(from: date)
}
class Utils {
  class func dateFormatter(_ date: Date, _ format: String) -> String {
    let dateformat = DateFormatter()
    dateformat.dateFormat = format
    return dateformat.string(from: date)
  }
}
print(Utils.dateFormatter(Date(), "EEEE, MMM d, yyyy"))

创建类名Utils导入相同的函数,您可以使用全局访问任何地方与您的日期和格式

Swift - 5.0

let date = Date()
let format = date.getFormattedDate(format: "yyyy-MM-dd HH:mm:ss") // Set output format

extension Date {
   func getFormattedDate(format: String) -> String {
        let dateformat = DateFormatter()
        dateformat.dateFormat = format
        return dateformat.string(from: self)
    }
}

Swift - 4.0

2018-02-01T19:10:04+00:00

extension Date {
    static func getFormattedDate(string: String , formatter:String) -> String{
        let dateFormatterGet = DateFormatter()
        dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        
        let dateFormatterPrint = DateFormatter()
        dateFormatterPrint.dateFormat = "MMM dd,yyyy"
        
        let date: Date? = dateFormatterGet.date(from: "2018-02-01T19:10:04+00:00")
        print("Date",dateFormatterPrint.string(from: date!)) // Feb 01,2018
        return dateFormatterPrint.string(from: date!);
    }
}