Monday 10 November 2014

Swift + NSDate ot String and Vice Versa



//--- Convert NSDate to Date String ---//

func dateformatterDate(date: NSDate) -> NSString
{
        var dateFormatter: NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"
        dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
        
        return dateFormatter.stringFromDate(date)
        

}





//--- Convert Date String to NSDate ---//

func dateformatterDateString(dateString: NSString) -> NSDate?
{
        var dateFormatter: NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"
        dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
        
        
        return dateFormatter.dateFromString(dateString)
 }

2 comments:

  1. You should have given an example with some string to which you are converting to date.
    How will you convert "2015-01-09 12:45:13" string into a date?

    ReplyDelete
  2. This seems to be returning the date after the one I have selected in the picker.. Is there something I may have overlooked?

    ReplyDelete