如何将此字符串“2016-04-14T10:44:00+0000”转换为NSDate并只保留年、月、日、小时?

中间的T真的让我在处理日期时不习惯。


当前回答

请使用ISO8601解析库来执行此操作。编码字符串的方法太多了。不要依赖于特定的格式,也不要依赖于始终发送相同的服务器。问题从末尾的“Z”开始,它将扩展到标准的所有品种。解析库将处理所有情况,并始终提供安全转换——而固定格式化字符串在将来可能会失败。

您可以使用这些库之一。CococaPods上也有:

https://github.com/boredzo/iso-8601-date-formatter/

https://github.com/malcommac/SwiftDate

看一下实现。它们都有几百行——这是有原因的。

关于这个问题:你可以使用NSDateComponents从日期中提取日期组件。网站上的例子正好符合你的情况。

https://developer.apple.com/documentation/foundation/nscalendar/1414841-components?language=objc

请注意,转换日期时会考虑到时区。你可能想显式地设置NSCalendar的locale。

其他回答

在swift4,

    var Msg_Date_ = "2019-03-30T05:30:00+0000"

    let dateFormatterGet = DateFormatter()
    dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = "MMM dd yyyy h:mm a"  //"MMM d, h:mm a" for  Sep 12, 2:11 PM
    let datee = dateFormatterGet.date(from: Msg_Date_)
    Msg_Date_ =  dateFormatterPrint.string(from: datee ?? Date())

    print(Msg_Date_)

//输出:- 2019年3月30日05:30 PM

Convert the ISO8601 string to date let isoDate = "2016-04-14T10:44:00+0000" let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" let date = dateFormatter.date(from:isoDate)! Get the date components for year, month, day and hour from the date let calendar = Calendar.current let components = calendar.dateComponents([.year, .month, .day, .hour], from: date) Finally create a new Date object and strip minutes and seconds let finalDate = calendar.date(from:components)


还可以考虑iOS 10 / macOS 10.12中引入的方便格式化程序ISO8601DateFormatter:

let isoDate = "2016-04-14T10:44:00+0000"

let dateFormatter = ISO8601DateFormatter()
let date = dateFormatter.date(from:isoDate)!

嗨,你有单独的T格式,然后转换为你喜欢

// create dateFormatter with UTC time format
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString("2015-04-01T11:42:00")

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
let timeStamp = dateFormatter.stringFromDate(date!)

请使用ISO8601解析库来执行此操作。编码字符串的方法太多了。不要依赖于特定的格式,也不要依赖于始终发送相同的服务器。问题从末尾的“Z”开始,它将扩展到标准的所有品种。解析库将处理所有情况,并始终提供安全转换——而固定格式化字符串在将来可能会失败。

您可以使用这些库之一。CococaPods上也有:

https://github.com/boredzo/iso-8601-date-formatter/

https://github.com/malcommac/SwiftDate

看一下实现。它们都有几百行——这是有原因的。

关于这个问题:你可以使用NSDateComponents从日期中提取日期组件。网站上的例子正好符合你的情况。

https://developer.apple.com/documentation/foundation/nscalendar/1414841-components?language=objc

请注意,转换日期时会考虑到时区。你可能想显式地设置NSCalendar的locale。

在Swift 4.1你可以做:

func getDate() -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.locale = Locale.current
    return dateFormatter.date(from: "2015-04-01T11:42:00") // replace Date String
}