获取日期和时间 Swift
Getting Date and Time Swift
我一直在研究检索 Swift 中的日期和时间,这是获得简短、可读的 Date/Time 输出的推荐策略:
let currentDate = NSDate()
let formatter = NSDateFormatter()
formatter.locale = NSLocale.currentLocale()
formatter.dateStyle = .ShortStyle
formatter.timeStyle = .ShortStyle
let convertedDate = formatter.dateFromString(currentDate) //ERROR HERE
print("\n\(convertedDate)")
但这会抛出一个异常,指出 currentDate
不是要传递的有效参数,因为它的类型是 NSDate
而不是 String
你能帮我理解为什么会这样吗?在检索日期和时间时,我只发现了类似的方法。非常感谢,感谢所有帮助!
你真的想从 NSDate
变成 String
,所以使用 stringFromDate
:
let convertedDate = formatter.stringFromDate(currentDate)
这里是你如何在 swift 3 语法中做到这一点 -
let currentDate = NSDate()
let formatter = DateFormatter()
formatter.locale = Locale.current
formatter.dateStyle = .short
formatter.timeStyle = .short
let convertedDate = formatter.string(from: currentDate as Date)
BR
Leo Dabus 在 Friso Buurman 的 Question 中为从 NSDate 获取字符串写了一个很好的扩展。奥布里加多狮子座
Mods请注意:我借鉴了它并重写了因为原始代码使用了相同的变量名来声明static constants
, string variables
和 argument parameters
这可能会让新编码员感到困惑。
这是 Leo 的一个很好的扩展,它主要包含 NSDateFormatter
样板代码,您可以在 Apple docs.
中找到
Swift 2
extension NSDateFormatter {
convenience init(stringDateFormat: String) {
self.init()
self.dateFormat = stringDateFormat
}
}
extension NSDate {
struct Formatter {
static let newDateFormat = NSDateFormatter(stringDateFormat: "dd-MM-yyyy")
}
var myNewDate: String {
return Formatter.newDateFormat.stringFromDate(self)
}
}
控制台输出:
print(NSDate().myNewDate) // "05-07-2016\n"
Swift 3
extension DateFormatter {
convenience init(stringDateFormat: String) {
self.init()
self.dateFormat = stringDateFormat
}
}
extension Date {
struct Formatter {
static let newDateFormat = DateFormatter(stringDateFormat: "dd-MM-yyyy")
}
var myNewDate: String {
return Formatter.newDateFormat.string(from: self)
}
}
控制台输出:
print(Date().myNewDate) // "05-07-2016\n"
我一直在研究检索 Swift 中的日期和时间,这是获得简短、可读的 Date/Time 输出的推荐策略:
let currentDate = NSDate()
let formatter = NSDateFormatter()
formatter.locale = NSLocale.currentLocale()
formatter.dateStyle = .ShortStyle
formatter.timeStyle = .ShortStyle
let convertedDate = formatter.dateFromString(currentDate) //ERROR HERE
print("\n\(convertedDate)")
但这会抛出一个异常,指出 currentDate
不是要传递的有效参数,因为它的类型是 NSDate
而不是 String
你能帮我理解为什么会这样吗?在检索日期和时间时,我只发现了类似的方法。非常感谢,感谢所有帮助!
你真的想从 NSDate
变成 String
,所以使用 stringFromDate
:
let convertedDate = formatter.stringFromDate(currentDate)
这里是你如何在 swift 3 语法中做到这一点 -
let currentDate = NSDate()
let formatter = DateFormatter()
formatter.locale = Locale.current
formatter.dateStyle = .short
formatter.timeStyle = .short
let convertedDate = formatter.string(from: currentDate as Date)
BR
Leo Dabus 在 Friso Buurman 的 Question 中为从 NSDate 获取字符串写了一个很好的扩展。奥布里加多狮子座
Mods请注意:我借鉴了它并重写了因为原始代码使用了相同的变量名来声明static constants
, string variables
和 argument parameters
这可能会让新编码员感到困惑。
这是 Leo 的一个很好的扩展,它主要包含 NSDateFormatter
样板代码,您可以在 Apple docs.
Swift 2
extension NSDateFormatter {
convenience init(stringDateFormat: String) {
self.init()
self.dateFormat = stringDateFormat
}
}
extension NSDate {
struct Formatter {
static let newDateFormat = NSDateFormatter(stringDateFormat: "dd-MM-yyyy")
}
var myNewDate: String {
return Formatter.newDateFormat.stringFromDate(self)
}
}
控制台输出:
print(NSDate().myNewDate) // "05-07-2016\n"
Swift 3
extension DateFormatter {
convenience init(stringDateFormat: String) {
self.init()
self.dateFormat = stringDateFormat
}
}
extension Date {
struct Formatter {
static let newDateFormat = DateFormatter(stringDateFormat: "dd-MM-yyyy")
}
var myNewDate: String {
return Formatter.newDateFormat.string(from: self)
}
}
控制台输出:
print(Date().myNewDate) // "05-07-2016\n"