如何将NSDate()分成几部分?

时间:2016-05-12 20:12:56

标签: swift nsdate nsdatecomponents

func date() -> String {
return NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.MediumStyle)
}

var date = date() // 2016. 5. 13. 오전 4:45:16

上面的代码,每次我调用date()

时,date都会获得韩国当前的日期值

我希望从NSDate()刷新新值,然后将刷新值插入varibles,如下面的代码

var yearMonDay = "2016. 5. 13"
var hourMinSec = "오전 4:45:16"

嗯有没有方法可以将NSDate()划分为如下代码所示的部分?

yearMonDay = NSDate().?? // refresh date "year: month: day"
hourMinSec = NSDate().?? // refresh date "am/fm hour:minute:second

2 个答案:

答案 0 :(得分:2)

可以使用NSCalendar的组件

来分割像小时这样的组件
let today       = NSDate()
let calendar    = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let components  = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: today)
print("\(components.month)      \(components.day)     \(components.year)")
print("\(components.hour)      \(components.minute)     \(components.second)")

使用NSDate格式化程序,您可以找到名称

let formatter   = NSDateFormatter()
let monthName = formatter.monthSymbols[components.month-1]
print(monthName)

答案 1 :(得分:1)

您可以使用NSDateFormatterStyle

return Promise.map(BigArray, function(innerArray) {
  return Promise.all(innerArray);
})
.then(function(finishedArray) {
  // code after all 60k promises have been resolved comes here
  console.log("done");
});