ios - How can I format date -
i date formatted 2015-07-15 17:12:00 +0000
, want display jul 15 2015.
how can that?
code:
func formatdate(date: string) -> string { if date != "" { var datestring = date datestring = datestring.substringtoindex(advance(minelement(indices(datestring)), 19)) datestring = datestring.stringbyreplacingoccurrencesofstring("", withstring: " ") var dateformatter = nsdateformatter() dateformatter.dateformat = "yyyy-mm-dd hh:mm:ss" var datestr = dateformatter.datefromstring(datestring) dateformatter.datestyle = nsdateformatterstyle.mediumstyle var date = dateformatter.stringfromdate(datestr!) return date } else { return "" } }
your input dateformatter string bit off - not include time zone , uses wrong hour pattern. following works , outputs
jul 15 2015
let datestr = "2015-07-15 17:12:00 +0000" var dateformatterin = nsdateformatter() dateformatterin.dateformat = "yyyy-mm-dd hh:mm:ss zzz" let date = dateformatterin.datefromstring(datestr) let dateformatterout = nsdateformatter() dateformatterout.dateformat = "mmm dd yyyy" print(dateformatterout.stringfromdate(date!))
for correct date strings take @ unicode docs
Comments
Post a Comment