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

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


当前回答

有时,在swift中将字符串转换为日期可能会导致返回nil,因此您应该在格式中添加“!”标记。日期函数!

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

let stringDate = "11-03-2018"
let date = dateFormatterUK.date(from: stringDate)!

其他回答

在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

在swift中使用字符串扩展将字符串转换为日期 Swift - 5.0

    extension String {
         public enum DateFormatType {
        
        /// The ISO8601 formatted year "yyyy" i.e. 1997
        case isoYear
        
        /// The ISO8601 formatted year and month "yyyy-MM" i.e. 1997-07
        case isoYearMonth
        
        /// The ISO8601 formatted date "yyyy-MM-dd" i.e. 1997-07-16
        case isoDate
        
        /// The ISO8601 formatted date and time "yyyy-MM-dd'T'HH:mmZ" i.e. 1997-07-16T19:20+01:00
        case isoDateTime
        
        /// The ISO8601 formatted date, time and sec "yyyy-MM-dd'T'HH:mm:ssZ" i.e. 1997-07-16T19:20:30+01:00
        case isoDateTimeSec
        
        /// The ISO8601 formatted date, time and millisec "yyyy-MM-dd'T'HH:mm:ss.SSSZ" i.e. 1997-07-16T19:20:30.45+01:00
        case isoDateTimeMilliSec
        
        /// The dotNet formatted date "/Date(%d%d)/" i.e. "/Date(1268123281843)/"
        case dotNet
        
        /// The RSS formatted date "EEE, d MMM yyyy HH:mm:ss ZZZ" i.e. "Fri, 09 Sep 2011 15:26:08 +0200"
        case rss
        
        /// The Alternative RSS formatted date "d MMM yyyy HH:mm:ss ZZZ" i.e. "09 Sep 2011 15:26:08 +0200"
        case altRSS
        
        /// The http header formatted date "EEE, dd MM yyyy HH:mm:ss ZZZ" i.e. "Tue, 15 Nov 1994 12:45:26 GMT"
        case httpHeader
        
        /// A generic standard format date i.e. "EEE MMM dd HH:mm:ss Z yyyy"
        case standard
        
        /// A custom date format string
        case custom(String)
        
        /// The local formatted date and time "yyyy-MM-dd HH:mm:ss" i.e. 1997-07-16 19:20:00
        case localDateTimeSec
        
        /// The local formatted date  "yyyy-MM-dd" i.e. 1997-07-16
        case localDate
        
        /// The local formatted  time "hh:mm a" i.e. 07:20 am
        case localTimeWithNoon
        
        /// The local formatted date and time "yyyyMMddHHmmss" i.e. 19970716192000
        case localPhotoSave
        
        case birthDateFormatOne
        
        case birthDateFormatTwo
        
        ///
        case messageRTetriveFormat
        
        ///
        case emailTimePreview
        
        var stringFormat:String {
          switch self {
          //handle iso Time
          case .birthDateFormatOne: return "dd/MM/YYYY"
          case .birthDateFormatTwo: return "dd-MM-YYYY"
          case .isoYear: return "yyyy"
          case .isoYearMonth: return "yyyy-MM"
          case .isoDate: return "yyyy-MM-dd"
          case .isoDateTime: return "yyyy-MM-dd'T'HH:mmZ"
          case .isoDateTimeSec: return "yyyy-MM-dd'T'HH:mm:ssZ"
          case .isoDateTimeMilliSec: return "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
          case .dotNet: return "/Date(%d%f)/"
          case .rss: return "EEE, d MMM yyyy HH:mm:ss ZZZ"
          case .altRSS: return "d MMM yyyy HH:mm:ss ZZZ"
          case .httpHeader: return "EEE, dd MM yyyy HH:mm:ss ZZZ"
          case .standard: return "EEE MMM dd HH:mm:ss Z yyyy"
          case .custom(let customFormat): return customFormat
            
          //handle local Time
          case .localDateTimeSec: return "yyyy-MM-dd HH:mm:ss"
          case .localTimeWithNoon: return "hh:mm a"
          case .localDate: return "yyyy-MM-dd"
          case .localPhotoSave: return "yyyyMMddHHmmss"
          case .messageRTetriveFormat: return "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
          case .emailTimePreview: return "dd MMM yyyy, h:mm a"
          }
        }  
 }
        
 func toDate(_ format: DateFormatType = .isoDate) -> Date?{
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format.stringFormat
        let date = dateFormatter.date(from: self)
        return date 
  }
 }

我们用like

    string.toDate(.isoDate)
    "1997-05-24".toDate(.isoDate)

试试下面的日期格式。

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = dateFormatter. dateFromString (strDate)

希望能有所帮助。

Swift 4.1:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = dateFormatter.date(from: strDate)

那SwiftDate呢?Swift最好的日期和时间解析和操作库。

https://github.com/malcommac/SwiftDate#1-date-parsing

使用可可豆荚安装: https://github.com/malcommac/SwiftDate/blob/master/Documentation/0.Informations.md#installation

然后:

import SwiftDate

// All default datetime formats (15+) are recognized automatically
let _ = "2010-05-20 15:30:00".toDate()
// You can also provide your own format!
let _ = "2010-05-20 15:30".toDate("yyyy-MM-dd HH:mm")
// All ISO8601 variants are supported too with timezone parsing!
let _ = "2017-09-17T11:59:29+02:00".toISODate()
// RSS, Extended, HTTP, SQL, .NET and all the major variants are supported!
let _ = "19 Nov 2015 22:20:40 +0100".toRSS(alt: true)

在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
}