Use Date Formatter iOS10 Swift3 - ios

How to use Date Formatter in ios 10 swift 3?
i

You'll have to first convert the string to a date and then to a string in your required format. You can use DateFormatter to do this. Check the below code
let date = "07-11-2016 12:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let convertedDate = formatter.date(from: date)
formatter.dateFormat = "d MMMM yyyy h a"
let dateString = formatter.string(from: convertedDate!)
print(dateString) // Output will be 7 November 2016 12 PM

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let date = dateFormatter.date(from: "07-11-2016 12:00:00")
dateFormatter.dateFormat = "dd MMMM yyyy HH a"
let dateString = dateFormatter.string(from: date)
dateString will be you desired output format of you date.
or you can simply create an extension of Date or string that return you the formatted date.

Related

Convert string to particular date format getting nil in iOS

I am getting date in string format from server. So I am trying to formatting to particular format.
But it is getting nil.
"07September16 4:09 am", This I want to format like Sep 07 2016
I am doing it with following code
dateLabel.text = model().formatStringToDate(dateString: date)
func formatStringToDate(dateString: String) -> String {
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d yyyy"
let formattedDate = dateFormatter.date(from: dateStr)
print("formattedDate \(formattedDate)")
//I have to return formatted string here
}
Any suggestions?
That should work;
// create `Date` from date string
let dateStr = "07September16 4:09 am"
let dateFormatterGet = DateFormatter()
dateFormatterGet.locale = Locale(identifier: "en_US_POSIX")
dateFormatterGet.dateFormat = "ddMMMMyy h:mm a"
let date = dateFormatterGet.date(from: dateStr)
// convert back Date to string as Sep 07 2016
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd yyyy"
print(dateFormatterPrint.string(from: date!))
let dateString = "07September16 4:09 am"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMyy h:mm a"
let dateObj = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "MMM dd yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
First you must get date from your string with current format like`
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMMyy h:mm a"
let formattedDate = dateFormatter.date(from: dateStr)!
print("formattedDate \(formattedDate)")
After getting the date you can get date string in which format you want just change the format`
dateFormatter.dateFormat = "MMM dd YYYY"
let convertedDateString = dateFormatter.string(from: formattedDate)

date is not showing after convert the string to date in swift

I have strings and converting into the date format, but when I convert after the date is showing properly but time is showing the wrong format
firsDateString : 2019-Mar-15 9:00 AM
secondName : 2019-Mar-15 8:00 PM
String convert into the date format
let addedDate = firsDateString
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: addedDate)
print("DATE \(date1)")
After that am getting this values:
DATE Optional(2019-03-15 03:30:00 +0000)
How can get the correct date format?
use like , but is not a valid answer may be it works based on your condition but follow option2 it will cover all scenario if you need
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: "2019-Mar-15 9:00 AM")
formatter.amSymbol = ""
formatter.pmSymbol = ""
let currentDateStr = formatter.string(from: date1!)
print("currentDateStr (currentDateStr)")
based on #JoakimDanielson point
How can this be the accepted answer when you no longer can tell the difference between 8 in the morning and 8 in the evening
here you need to follow the 24 hour format , I update my answer ,
Option 2
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: "2019-Mar-15 9:00 AM")
formatter.dateFormat = "yyyy-MMM-dd HH:mm"
let currentDateStr = formatter.string(from: date1!)
print("currentDateStr \(currentDateStr)")
Output
Check this:
let addedDate = firsDateString
let formatter = DateFormatter()
formatter.timeZone = (NSTimeZone(name: "UTC")! as TimeZone)
formatter.dateFormat = "yyyy-MMM-dd hh:mm a"
let date1 = formatter.date(from: addedDate)
print("DATE \(String(describing: date1!))")

Format Date from String - swift3

HI I am trying to formate date from string (08-05-1988) I want to convert to date as given below, output also given below,
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
let stringValue = String(describing: dateFromString)
out put : Optional(1988-05-07 18:30:00 +0000)
Then, I want to format the out put date from (1988-05-07 18:30:00 +0000) to other format as given below. But, when format the string to Date is nil
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
let formatter = DateFormatter()
formatter.dateFormat = "dd-MMM-yyyy"
let formatedDate:String = formatter.string(from: date!)
Expected Output : 08-May-1988
The output Optional(1988-05-07 18:30:00 +0000) is the result of the description of a Date object. It's not related to any given string format.
To convert 08-05-1988 to 08-May-1988 you can simply use
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd-MMM-yyyy"
let formatedDate = dateFormatter.string(from: dateFromString!)
let date = Date()
let format = DateFormatter()
format.dateFormat = "dd-MM-yyyy"
let formatDate = format.string(from: date)
You're calling dateFormatter.date on dateString instead of dateFromString. Since "08-05-1988" doesn't match the specified format of "yyyy-mm-dd hh:mm:ss Z", dateFormatter.date returns nil.

Trying to reformat a string to a specific date format

I have a string that is a date in this format 03/02/2017
I would like to display it like this Mar 2, 2017
I've been trying to get this formatter to work, but it keeps giving me nil, and I don't think I'm setting it in the correct format.
// convert saved date from string
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "mm/dd/yyyy" //Your date format
let date = dateFormatter.date(from: '03/02/2017')
print("\(date) save date") // nil
historyEntry.date = date as NSDate?
is not in
dateFormatter.dateFormat = "mm/dd/yyyy"
it is in
dateFormatter.dateFormat = "MM/dd/yyyy"
for full output
let dateFormatter = DateFormatter()
// iinitiallly set the date format based on your date
dateFormatter.dateFormat = "MM/dd/yyyy" //Your date format
// convert the string to date , but date is not the proper format , so dont worry about this
let date = dateFormatter.date(from: "03/02/2017")
// again reassign what the output you need set the date format
dateFormatter.dateFormat = "MMM dd, yyyy" // if you need the same as Mar 2, 2017 then use "MMM d, yyyy"
// finally convert your date to string
let myStringafd = dateFormatter.string(from: date!)
print("\(myStringafd) save date")
output
let dateString = "03/02/2017"
let df = DateFormatter()
df.dateFormat = "MM/dd/yyyy" // this dateformat should be your current string's date format
df.timeZone = TimeZone(identifier: "UTC-05:00") // if there is timezone you want to set
if let date = df.date(from: dateString) {
df.dateFormat = "MMM dd, yyyy" // This dateformat is that you want as result
let stringFromDate = df.string(from: date)
cell.lblDate.text = stringFromDate
}

Found nil from dateFromString when converting string to date

Here is my code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM, yyyy"
let myDate = dateFormatter.dateFromString("2016-06-10")
print(myDate)
myDate is nil when I print.
If you want to convert this "2016-06-10" in to "dd MM, yyyy" then you need to write it like below code,
let dateString = "2016-06-10"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.dateFromString(dateString)
formatter.dateFormat = "dd MMM, yyyy"
let st = formatter.stringFromDate(date!)
print(st) //output : 10 Jun, 2016
Hope this helps you.

Resources