在swift中,我需要显示 “10月02日”
**而不是** "10月2日"
,如何转移 "2"to "02"
?
现在,我已经写了这些代码,但它会显示" 2"但不会" 02":
var publishLastDate:NSDate = momentLast.created_on
println(publishLastDate)
componentsLast = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear, fromDate: publishLastDate)
lastDay = componentsLast.day
lastMonth = componentsLast.month
lastYear = componentsLast.year
答案 0 :(得分:1)
看起来你正在抓住组件并自己打印字符串。你可以这样格式化:
let string = String(format: "%02d月%02d日", lastMonth, lastDay)