我想获得当前日期为SEP 15,2015。我尝试首先从NSDate中删除时间
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate: date)
let currentdate = NSCalendar.currentCalendar().dateFromComponents(components)
println(currentdate)
然而,这并没有截断时间也使用了这个方法,它给了我个别的组件
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate: date)
let year = components.year
let month = components.month
let day = components.day
let monthname = components.weekday
但问题是我使用第二种方法获得了int
中的月份。我希望将1
作为January
进行通信。我如何实现我的结果?
答案 0 :(得分:1)
您可以使用NSDateFormatter格式化您的日期
let formatter = NSDateFormatter()
formatter.dateFormat = "MMM dd,yyyy"
print(formatter.stringFromDate(NSDate()))
答案 1 :(得分:1)
以下是以所需格式打印当前日期的代码:
*(&y + 1)
答案 2 :(得分:0)
对不起这个问题..我应该了解dateFormatter
。我从号码获得了月份名称,所以问题解决了,但是如果有更好的方法可以做到这一点。请说
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitDay | .CalendarUnitMonth | .CalendarUnitYear, fromDate: date)
let year = components.year
let month = components.month
let day = components.day
let dateFormatter: NSDateFormatter = NSDateFormatter()
let months = dateFormatter.shortMonthSymbols
let monthSymbol = months[month-1] as! String
println(monthSymbol)