Swift 2.0 String to NSDate

青春壹個敷衍的年華 提交于 2019-12-13 10:26:13

问题


Hello i get my date from Datepicker that gets saved to a string then uploaded to Firebase. The string is then recieved to the phone. The problem is that i want to convert this string to NSDate when i retrieve it.

This is how i get a string from datepicker

func datePickerChanged(datePicker:UIDatePicker){
    var dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

    var strDateFrom = dateFormatter.stringFromDate(datePicker.date)
    fromDate = strDateFrom
    print(fromDate)}

When i retrieve the date i get it as a string this is the print

print(self.membershipActiveTo)

And this is the print log 5/11/16, 2:35 PM

And below is the line of code i have tried to convert to string but it only returns nil

let strDate = self.membershipActiveTo // "2015-10-06T15:42:34Z"
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
        print ( dateFormatter.dateFromString( strDate ) )

回答1:


There are things to consider when working with dates and one of them is how to store the date in a consistent format that can be easily worked with for different uses. In your case, you may want to sort by date and if the data is stored like this

5/11/16, 2:35 PM

It's not going to be sortable. So, a different format is needed and here's one possible example

20160511143500

Here's some code to manipulate dates:

Write a nicely formatted date to Firebase

let d = NSDate()

//create a custom date string & save in firebase
let dateFormatterCustom = NSDateFormatter()
dateFormatterCustom.dateFormat = "yyyyMMddhhmmss"

let customDateString = dateFormatterCustom.stringFromDate(d)
print(customDateString) //prints "20160511023500" just for testing

let timesRef = self.myRootRef.childByAppendingPath("time_test")
let thisTimeRef = timesRef.childByAutoId()
let timeChildRef = thisTimeRef.childByAppendingPath("timestamp")

timeChildRef.setValue(customDateString)

Firebase now has this

time_test  
 -KFerAcANQv4pN1Pp4yW
    timestamp: "20160418033309"

Then to read in from Firebase:

    let timesRef = self.myRootRef.childByAppendingPath("time_test")

    timesRef.observeEventType(.ChildAdded, withBlock: { snapshot in

        let timeStamp = snapshot.value.objectForKey("timestamp") as! String
        print(timeStamp) //still looks like 20160418033309

        let shortDateString = self.timeStampToDateString(timeStamp)
        print(shortDateString) //prints 4/18/16, 3:33 AM

    })

and the function that converts the timestamp style string to a human readable one

func timeStampToDateString(stamp: String) -> String {

    //create a custom date string & create a date object from it
    let dateFormatterCustom = NSDateFormatter()
    dateFormatterCustom.locale = NSLocale(localeIdentifier: "US_en")
    dateFormatterCustom.dateFormat = "yyyyMMddhhmmss"

    let d = dateFormatterCustom.dateFromString(stamp)!

    //create a short date style string and format the string
    let dateFormatterShortStyle  = NSDateFormatter()
    dateFormatterShortStyle.dateStyle = NSDateFormatterStyle.ShortStyle
    dateFormatterShortStyle.timeStyle = NSDateFormatterStyle.ShortStyle

    let dateString = dateFormatterShortStyle.stringFromDate(d)

    return dateString
}

There's a lot of extra code in this example so it could be shortened considerably but I wanted to leave in all of the steps.



来源:https://stackoverflow.com/questions/36695343/swift-2-0-string-to-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!