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

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


当前回答

斯威夫特5.6

这是一个简单的方法来获得一个字符串,并将其转换回字符串与日期的结构通过字符串扩展

日期字符串示例"2022-03-15T07:13:49.607+00:00"

如果日期的格式不是“yyyy-MM-dd'T hh:mm:ss”。SSSSSSSZ” 它将返回01-01-70

import Foundation

extension String {

  func convertToDate() -> String {
   
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"
      if let dt = dateFormatter.date(from: self) {
          dateFormatter.dateFormat = "MM-dd-yy"
          let formatedStringDate = dateFormatter.string(from: dt)
              return formatedStringDate
      }
    
   
      return "01-01-70"
  }
}

其他回答

斯威夫特5。看是否一个日期已经通过:

let expiryDate = "2020-01-10" // Jan 10 2020

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

if Date() < dateFormatter.date(from: expiryDate) ?? Date() {
    print("Not Yet expiryDate")
} else {
    print("expiryDate has passed")
}

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)!

我对这种格式也很着迷。

请参阅下面的解决方案。

你的字符串来自你的背部或其他来源:

    let isoDate = "2020-05-06 20:00:00-03"

识别日期格式

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ssZ"
    let date = dateFormatter.date(from:isoDate)!

现在您已经将日期设置为date(),您可以使用formatDate.string将其更改为您想要的任何格式

    let formatDate = DateFormatter()
    formatDate.dateFormat = "dd/MM/yyyy HH:mm"
    let drawDate = formatDate.string(from: date)
    print(drawDate)

输出:

    06/05/2020 20:00

只需要传递dateformate和date,就可以得到年,月,日,小时。额外的信息

func GetOnlyDateMonthYearFromFullDate(currentDateFormate:NSString , conVertFormate:NSString , convertDate:NSString ) -> NSString
    {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = currentDateFormate as String
        let formatter = NSDateFormatter()
        formatter.dateFormat = Key_DATE_FORMATE as String
        let finalDate = formatter.dateFromString(convertDate as String)
        formatter.dateFormat = conVertFormate as String
        let dateString = formatter.stringFromDate(finalDate!)

        return dateString
    }

获得一年

let Year = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "YYYY", convertDate: "2016-04-14T10:44:00+0000") as String

让月

let month = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "MM", convertDate: "2016-04-14T10:44:00+0000") as String

得到一天

let day = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "dd", convertDate: "2016-04-14T10:44:00+0000") as String

把小时

let hour  = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "hh", convertDate: "2016-04-14T10:44:00+0000") as String

就用SwifterSwift吧。

stringDate = "2020-04-26T08:56:17.987Z"
let date = Date(iso8601String: stringDate)