How can I convert string date to NSDate?

前端 未结 18 1700
花落未央
花落未央 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:34

    For Swift 3

    func stringToDate(_ str: String)->Date{
        let formatter = DateFormatter()
        formatter.dateFormat="yyyy-MM-dd hh:mm:ss Z"
        return formatter.date(from: str)!
    }
    func dateToString(_ str: Date)->String{
        var dateFormatter = DateFormatter()
        dateFormatter.timeStyle=DateFormatter.Style.short
        return dateFormatter.string(from: str)
    }
    
    0 讨论(0)
  • 2020-11-22 16:37

    To add String within Date Format in Swift, I did this

     var dataFormatter:NSDateFormatter = NSDateFormatter()
                    dataFormatter.dateFormat = "dd-MMMM 'at' HH:mm a"
    
    cell.timeStamplbl.text = dataFormatter.stringFromDate(object.createdAt)
    
    0 讨论(0)
  • 2020-11-22 16:37
    Swift: iOS
    if we have string, convert it to NSDate,
    
    var dataString = profileValue["dob"] as String
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"
    
    // convert string into date
    let dateValue:NSDate? = dateFormatter.dateFromString(dataString)
    
    if you have and date picker parse date like this
    
    // to avoid any nil value
    if let isDate = dateValue {
    self.datePicker.date = isDate
    }
    
    0 讨论(0)
  • 2020-11-22 16:38

    Swift support extensions, with extension you can add a new functionality to an existing class, structure, enumeration, or protocol type.

    You can add a new init function to NSDate object by extenging the object using the extension keyword.

    extension NSDate
    {
        convenience
        init(dateString:String) {
            let dateStringFormatter = NSDateFormatter()
            dateStringFormatter.dateFormat = "yyyyMMdd"
            dateStringFormatter.locale = NSLocale(localeIdentifier: "fr_CH_POSIX")
            let d = dateStringFormatter.dateFromString(dateString)!
            self.init(timeInterval:0, sinceDate:d)
        }
    } 
    

    Now you can init a NSDate object using:

    let myDateObject = NSDate(dateString:"2010-12-15 06:00:00")
    
    0 讨论(0)
  • 2020-11-22 16:39

    Below are some string to date format converting options can be usedin swift iOS.

    1. Thursday, Dec 27, 2018 format= EEEE, MMM d, yyyy
    2. 12/27/2018 format= MM/dd/yyyy
    3. 12-27-2018 09:59 format= MM-dd-yyyy HH:mm
    4. Dec 27, 9:59 AM format= MMM d, h:mm a
    5. December 2018 format= MMMM yyyy
    6. Dec 27, 2018 format= MMM d, yyyy
    7. Thu, 27 Dec 2018 09:59:19 +0000 format= E, d MMM yyyy HH:mm:ss Z
    8. 2018-12-27T09:59:19+0000 format= yyyy-MM-dd'T'HH:mm:ssZ
    9. 27.12.18 format= dd.MM.yy
    10. 09:59:19.815 format= HH:mm:ss.SSS
    0 讨论(0)
  • 2020-11-22 16:44

    If you're going to need to parse the string into a date often, you may want to move the functionality into an extension. I created a sharedCode.swift file and put my extensions there:

    extension String
    {   
        func toDateTime() -> NSDate
        {
            //Create Date Formatter
            let dateFormatter = NSDateFormatter()
    
            //Specify Format of String to Parse
            dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
    
            //Parse into NSDate
            let dateFromString : NSDate = dateFormatter.dateFromString(self)!
    
            //Return Parsed Date
            return dateFromString
        }
    }
    

    Then if you want to convert your string into a NSDate you can just write something like:

    var myDate = myDateString.toDateTime()
    
    0 讨论(0)
提交回复
热议问题