November 为 Date 和 DateFormatter 提供了一系列扩展,以构建一个简洁且易于使用的 API 来简化日期到字符串的转换。
创建一个 DateFormatter
是一个耗时的操作。因此,November 会将其创建的 DateFormatter
缓存起来,以便在整个应用程序生命周期中重复使用。
本项目旨在提供一组 API 来从 Date
实例获取字符串表示形式。
let stringWithStyles = date.string(dateStyle: .medium, timeStyle: .none)
let stringWithTemplate = date.string(withTemplate: "MMMMyyyy")
let stringWithFormat = date.string(withFormat: "MM/yy")
let monthAndYearString = date.string(with: .monthAndYear)
继续阅读以了解如何实现!
这是库的 Swift 3 版本。您可以在此处查看 Swift 2 版本:这里。
将以下内容添加到您的 Podfile
中
pod 'November'
然后运行 $ pod install
。
最后,在需要 November 的类中
import November
如果您还没有安装或集成 CocoaPods,您可以从这里学习如何进行操作。
将 Date
转换为 String
有三种主要方法。
let date = Date()
let string = date.string(dateStyle: .shortStyle, timeStyle: .shortStyle)
如有必要,您可以提供自定义区域设置以执行转换
let spanishLocale = Locale(localeIdentifier: "es")
let date = Date()
let string = date.string(dateStyle: .mediumStyle, timeStyle: .noStyle, locale: spanishLocale)
let date = Date()
let string = date.string(withTemplate: "MMMyyyy")
如有必要,您可以提供自定义区域设置以执行转换
let spanishLocale = Locale(localeIdentifier: "es")
let date = Date()
let string = date.string(withTemplate: "MMMyyyy", locale: spanishLocale)
let date = Date()
let string = date.string(withFormat: "MM/dd/yyyy HH:mm:ss")
如有必要,您可以提供自定义区域设置以执行转换
let spanishLocale = Locale(localeIdentifier: "es")
let date = Date()
let string = date.string(withFormat: "MM/dd/yyyy HH:mm:ss", locale: spanishLocale)
同样,有三种方法可以从 String
创建一个 Date
。它们作为 Date
初始化器提供
let string = "11/18/83, 11:30 AM"
let convertedDate = Date(string: string, dateStyle: .shortStyle, timeStyle: .shortStyle)
注意:转换后的日期是Optional
,如果字符串无法解析,则将为nil
如有必要,您可以提供自定义区域设置以执行转换
let spanishLocale = Locale(localeIdentifier: "es")
let string = "18/11/83 11:30"
let convertedDate = Date(string: string, dateStyle: .shortStyle, timeStyle: .shortStyle, locale: spanishLocale)
let string = "November 18, 1983, 11:30"
let convertedDate = Date(string: string, template: "ddMMMMyyyyHHmm")
注意:转换后的日期是Optional
,如果字符串无法解析,则将为nil
如有必要,您可以提供自定义区域设置以执行转换
let spanishLocale = Locale(localeIdentifier: "es")
let string = "noviembre 18, 1983, 11:30"
let convertedDate = Date(string: string, template: "ddMMMMyyyyHHmm", locale: spanishLocale)
let string = "18/November/1983 11:30"
let convertedDate = Date(string: string, format: "dd/MMMM/yyyy HH:mm")
注意:转换后的日期是Optional
,如果字符串无法解析,则将为nil
如有必要,您可以提供自定义区域设置以执行转换
let spanishLocale = Locale(localeIdentifier: "es")
let string = "18/noviembre/1983 11:30"
let convertedDate = Date(string: string, format: "dd/MMMM/yyyy HH:mm", locale: spanishLocale)
在我的应用中,我喜欢这样使用November
通常我只有一套模板和/或格式要使用。我将它们包装在一组枚举中
enum DateTemplate: String {
case monthAndYear = "MMMyyyy"
case fullShortDate = "ddMMyy"
}
enum DateFormat: String {
case fullDate = "dd/MM/yyyy"
case fullDateAndTime = "dd/MM/yyyy HH:mm:ss"
}
我创建了一个扩展,将November
方法包装起来,以接受定义的枚举值
extension Date {
// MARK: Helpers Date -> String
func string(with template: DateTemplate) -> String {
return string(withTemplate: template.rawValue)
}
func string(with format: DateFormat) -> String {
return string(withFormat: format.rawValue)
}
// MARK: Helpers String -> Date
convenience init?(string: String, template: DateTemplate) {
self.init(string: string, template: template.rawValue)
}
convenience init?(string: String, format: DateFormat) {
self.init(string: string, format: format.rawValue)
}
}
现在,我们有一个简洁、优雅的API来将日期转换为字符串以及相反的过程
let date = Date()
// Templates
let monthAndYearString = date.string(with: .monthAndYear)
let fullShortDateString = date.string(with: .fullShortDate)
let dateFromMonthAndYear = Date(string: "11/1983", template: .monthAndYear)
// Formats
let fullDateString = date.string(with: .fullDate)
let fullDateAndTimeString = date.string(with: .fullDateAndTime)
let dateFromfullDateString = Date(string: "11/18/1983", format: .fullDate)
提供的三种字符串格式化方法还不够吗?不必担心,你仍然可以利用November
。
如果你想要进一步自定义格式化程序,你可以使用DateFormatterProvider
协议。
为了遵循此协议,你必须重写一个属性(cacheKey
)和一个函数(configure(_: DateFormatter)
)。这里有一个例子:
class MyDateFormatterProvider: DateFormatterProvider {
let cacheKey: String
let format: String
init(format: String) {
self.format = format
self.cacheKey = "MyConfigurator(\(format))"
}
func configure(formatter: DateFormatter) {
formatter.dateFormat = format
formatter.monthSymbols = ["JN", "FB", "MR", "AP", "MY", "JN", "JL", "AG", "SP", "OT", "NV", "DC"]
// whatever configuration you need
}
}
然后,稍后
let myProvider = MyDateFormatterProvider(format: "dd MMMM yyyy")
// from date to string
let date = Date()
let myCustomFormatString = date.string(with: myProvider)
// from string to date
let string = "18 NV 1983"
let convertedDate = Date(string: string, provider: myProvider)
用于序列化日期或字符串的DateFormatter
将按定义的cacheKey
缓存,如果再次需要,将被很好地重用。换句话说,如果在应用程序的任何其他点我们创建了具有相同格式的新的提供者
let otherProvider = MyDateFormatterProvider(format: "dd MMMM yyyy")
// from date to string
let otherDate = Date()
let otherCustomFormatString = date.string(with: otherProvider)
则将重用DateFormatter
。
如果你不介意缓存格式化程序,你可以简单地使用日期扩展通过使用DateFormatter将日期从转换为字符串及反之亦然
let formatter = DateFormatter()
formatter.dateFormat = "MM/yyyy"
let string = Date().string(with: formatter)
let date = Date(string: "06/2016, formatter: formatter")
Manuel García-Estañ Martínez
@manueGE
November可在MIT许可证下使用。