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
}
Related
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)
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.
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.
From response I have set time in String, its contains HH: mm format the task is I have to convert that string into 12hrs time interval with AM and PM in another string to display results
For example "16:36:00.000Z" I want my result like 4:36 PM
Here my sample code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString(dateValue)
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "HH:mm"
let dateString = dateFormatter.stringFromDate(date!)
print(dateString)
But unfortunately I'm getting nil in date
do like
let dateFormatter = NSDateFormatter()
16:36:00.000Z
dateFormatter.dateFormat = "HH:mm:ss.sssZ"
let date = dateFormatter.dateFromString(dateValue)
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "hh:mm a" // or //h:mm a
let dateString = dateFormatter.stringFromDate(date!)
print(dateString)
GOAL: I want my string to be date formatted and put inside a label.
Current code:
let dateString = "2015-10-09 17:41:14"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy | hh:mm a"
let date = dateFormatter.dateFromString(dateString)
//FOUND NIL HERE
let NewString = dateFormatter.stringFromDate(date!)
Date.text = NewString
The code above works if I do NSDate() but not if I use my own string?
You are using wrong date format to convert the string to date and it returns a nil date because of that. First you need to use the exact date format to convert the string to date then set your date formatter style to get it back as string in your desired format.
Change your code like:
let dateString = "2015-10-09 17:41:14"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString(dateString)
dateFormatter.dateFormat = "MMMM d, yyyy | hh:mm a"
let resultString = dateFormatter.stringFromDate(date!)
print(resultString) // October 9, 2015 | 05:41 PM