How can I convert string date to NSDate?

前端 未结 18 1684
花落未央
花落未央 2020-11-22 16:14

I want to convert \"2014-07-15 06:55:14.198000+00:00\" this string date to NSDate in Swift.

相关标签:
18条回答
  • 2020-11-22 16:52

    You can try this swift code

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy"//same as strDate date formator
        dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")//Must used if you get one day less in conversion
        let convertedDateObject = dateFormatter.date(from: strDate)
    
    0 讨论(0)
  • 2020-11-22 16:53

    SWIFT 5, Xcode 11.0

    Pass your (date in string) in "dateString" and in "dateFormat" pass format you want. To choose format, use NDateFormatter website.

    func getDateFrom(dateString: String, dateFormat: String) -> Date? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = dateFormat
        dateFormatter.locale = Locale(identifier: "en_US")
        guard let date = dateFormatter.date(from: dateString) else {return nil}
        return date
    }
    
    0 讨论(0)
  • 2020-11-22 16:55

    Since Swift 3, many of the NS prefixes have been dropped.

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" 
    /* date format string rules
     * http://userguide.icu-project.org/formatparse/datetime
     */
    
    let date = dateFormatter.date(from: dateString)
    
    0 讨论(0)
  • 2020-11-22 16:56
     func convertDateFormatter(date: String) -> String
     {
    
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
        dateFormatter.timeZone = NSTimeZone(name: "UTC")
        let date = dateFormatter.dateFromString(date)
    
    
        dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"///this is what you want to convert format
        dateFormatter.timeZone = NSTimeZone(name: "UTC")
        let timeStamp = dateFormatter.stringFromDate(date!)
    
    
        return timeStamp
    }
    

    Updated for Swift 3.

    func convertDateFormatter(date: String) -> String
    {
    
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
        dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
        let date = dateFormatter.date(from: date)
    
    
        dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"///this is what you want to convert format
        dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
        let timeStamp = dateFormatter.string(from: date!)
    
    
        return timeStamp
    }
    
    0 讨论(0)
  • 2020-11-22 16:57

    Swift 4

    import Foundation
    
    let dateString = "2014-07-15" // change to your date format
    
    var dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    
    let date = dateFormatter.date(from: dateString)
    println(date)
    

    Swift 3

    import Foundation
    
    var dateString = "2014-07-15" // change to your date format
    
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    
    var date = dateFormatter.dateFromString(dateString)
    println(date)
    

    I can do it with this code.

    0 讨论(0)
  • 2020-11-22 16:58

    try this:

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = /* find out and place date format from 
                                * http://userguide.icu-project.org/formatparse/datetime
                                */
    let date = dateFormatter.dateFromString(/* your_date_string */)
    

    For further query, check NSDateFormatter and DateFormatter classes of Foundation framework for Objective-C and Swift, respectively.

    Swift 3 and later (Swift 4 included)

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = /* date_format_you_want_in_string from
                                * http://userguide.icu-project.org/formatparse/datetime
                                */
    guard let date = dateFormatter.date(from: /* your_date_string */) else {
       fatalError("ERROR: Date conversion failed due to mismatched format.")
    }
    
    // use date constant here
    
    0 讨论(0)
提交回复
热议问题