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.
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)
I'm trying to convert following date/time: "04-26-2018 10:17 AM" to this format: "2018-04-26T10:17"
here's my code:
let myDateString = "04-26-2018 10:17 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm a"
let myDate = dateFormatter.date(from: myDateString)!
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
let somedateString = dateFormatter.string(from: myDate)
The output for somedateString is "2018-04-26T00:17" which is incorrect.. is there something wrong in my code?
you just need correct DateFormatter this MM-dd-yyyy HH:mm a to MM-dd-yyyy hh:mm a
let myDateString = "04-26-2018 10:17 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy hh:mm a"
let myDate = dateFormatter.date(from: myDateString)!
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
let somedateString = dateFormatter.string(from: myDate)
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.
I have encountered with below lines of code sometimes doesn't return proper UTC value:
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = NSTimeZone(abbreviation: "UTC")
Returns:
"2016-12-11 2:07:35 am +0000"
Instead of:
"2016-12-11 02:07:35 +0000"
What's wrong with that?
please use these code
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = NSTimeZone.localTimeZone()
formatter.locale = NSLocale.currentLocale()
print(formatter.stringFromDate(date))
why does this return a nil? detailDate is in this format:
"Wednesday, November 23, 2016 at 2:01 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
dateFormatter.dateStyle = .long
let dateForCalendar = dateFormatter.date(from: detailDate!)
I believe it returns a nil because my dateFormat is not the same as what my detailDate format is, but I don't understand why it's different.
After assigning a DateFormat why are you changing the date style ?
remove,
dateFormatter.dateStyle = .long
and it will work,
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
let dateForCalendar = dateFormatter.date(from: detailDate!)
let detailDate = "Wednesday, November 23, 2016 at 2:01 PM"
let dateformatter = DateFormatter()
dateformatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
let strDate = dateformatter.date(from: detailDate)
print(strDate!)
try this , it's work proper