Converting a date string from one date format to another date format is an easy by using the following method:
Thursday, Oct 12, 2017 - EEEE, MMM d, yyyy
10/12/2017 - MM/dd/yyyy
10-12-2017 09:48 - MM-dd-yyyy HH:mm
Oct 12, 9:48 AM - MMM d, h:mm a
October 2017 - MMMM yyyy
Oct 12, 2017 - MMM d, yyyy
Thu, 12 Oct 2017 09:48:59 +0000 - E, d MMM yyyy HH:mm:ss Z
2017-10-12T09:48:59+0000 - yyyy-MM-dd'T'HH:mm:ssZ
12.10.17 - dd.MM.yy
For more check here.
The inputDateFormat should be same as dateString, but the ouputDateFormat can be different as we required.
Date Formats:
Following are the some Date Formats:Thursday, Oct 12, 2017 - EEEE, MMM d, yyyy
10/12/2017 - MM/dd/yyyy
10-12-2017 09:48 - MM-dd-yyyy HH:mm
Oct 12, 9:48 AM - MMM d, h:mm a
October 2017 - MMMM yyyy
Oct 12, 2017 - MMM d, yyyy
Thu, 12 Oct 2017 09:48:59 +0000 - E, d MMM yyyy HH:mm:ss Z
2017-10-12T09:48:59+0000 - yyyy-MM-dd'T'HH:mm:ssZ
12.10.17 - dd.MM.yy
For more check here.
One Date Format To Another:
For converting we are going to create String extension as follow:extension String { func toDateString( inputDateFormat inputFormat : String, ouputDateFormat outputFormat : String ) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = inputFormat let date = dateFormatter.date(from: self) dateFormatter.dateFormat = outputFormat return dateFormatter.string(from: date!) } }
Usage:
Add the following lines of code for usage:
let dateString = "2017-10-10 15:56:25" let requiredFormat = dateString.toDateString(inputDateFormat: "yyyy-MM-dd HH:mm:ss", ouputDateFormat: "dd 'at' hh:mm:ss") print(requiredFormat) //output will be '10 at 03:56:25'
The inputDateFormat should be same as dateString, but the ouputDateFormat can be different as we required.
No comments:
Post a Comment