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

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

above code, date obtain korean current value of date every time that i call date()

I'll hope to have refresh new value from NSDate(), and so insert refresh new value into varibles like below code

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

hmmm Are there methods to divide NSDate() into pieces like below code?

yearMonDay = NSDate().?? // refresh date "year: month: day"
hourMinSec = NSDate().?? // refresh date "am/fm hour:minute:second
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
WoderMan
  • 53
  • 1
  • 12
  • Consider reformulating the question. Isn't what you want to achieve something you can do with NSDateComponents? – catalandres May 12 '16 at 20:25

2 Answers2

2

Splitting up components like hour can be done using the components of the 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)")

With the NSDate formatter you can find the names

let formatter   = NSDateFormatter()
let monthName = formatter.monthSymbols[components.month-1]
print(monthName)
Philip De Vries
  • 417
  • 4
  • 13
  • `NSDate(timeIntervalSinceNow: 0)`??? please just use `NSDate()`. Besides that you can use NSDateFormatter to get the month name check this http://stackoverflow.com/a/27369380/2303865 – Leo Dabus May 12 '16 at 21:38
1

You can use the NSDateFormatterStyle:

// get the current date and time
let currentDateTime = NSDate()

// initialize the date formatter and set the style
let formatter = NSDateFormatter()

// October 26, 2015
formatter.timeStyle = NSDateFormatterStyle.NoStyle
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.stringFromDate(currentDateTime)

// 6:00:50 PM
formatter.timeStyle = NSDateFormatterStyle.MediumStyle
formatter.dateStyle = NSDateFormatterStyle.NoStyle
formatter.stringFromDate(currentDateTime)
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133