最近遇到一个问题。一个关于时间的 UI 显示,须要显示上午/下午
。通常来讲,就是在 DateFormatter 里面进行设置 dateFormat 便可。可是通常都是AM/PM
。后来发现 iOS/macOS 比较均可以显示上午/下午的。苹果的开发团队不可能还傻到得本身去算时间吧。因而看 DateFormmatter 的文档,发现了 Locale 这个东西。html
说了这么多废话。总算进主题了。该篇是整理下 DateFormatter。老司机就不用往下看了。?。swift
DateFormatter 的使用比较简单。通常的话,很常见的用法就是用 DateFormatter 将 Date 和 String 互换。看?。app
let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "YYYY年MM月dd日 HH:mm:ss" dateFormatter.string(from: date)
dateFormat
的格式,实际上是遵照Unicode Technical Standard
。ide
To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35. The version of the standard varies with release of the operating system:OS X v10.9 and iOS 7 use version tr35-31.ui
OS X v10.8 and iOS 6 use version tr35-25.code
不一样系统版本遵循的格式范围不一样。不过如今广泛都是 iOS 7 以上适配了。orm
在 Unicode Technical Standard 中,Date Format Patterns支持的格式挺多的。这里只列一些经常使用的。htm
Field | symbol | Description |
---|---|---|
年份 | y | 正常的年份显示,能够用多种组合。 |
U | 表示农历年份,干支纪法。好比 甲子、丁酉。可是须要配合Calendar 和Locale 使用 |
|
月份 | M | 表示 monthSymbols。 M/MM 表示月份的数字,如 1/01;MMM 表示简写月份,对应着shortMonthSymbols ,如 Jun;MMMM 表示标准月份,对应着monthSymbols ,如 June;MMMMM 表示最简的月份表示,对应着veryShortMonthSymbols ,如一月份是 J |
L | 表示 standaloneMonthSymbols。类同 M 的表示方法。 | |
天数 | d | 表示一个月中的第几天,d/dd 表示 1/01 的区别 |
D | 表示一年中的第几天,可D/DD/DDD |
|
周 | w | 表示一年中的第几周,可w/ww |
W | 表示一个月中的第几周,可W |
|
E | 表示周几这样。好比星期日。E\EE\EEE ->Sun, EEEE ->Sunday, EEEEE ->S。还能够搭配 Calendar 和 Locale,显示中文的 周日/星期日/日 |
|
c | 同 E。可是主要是是对应standaloneWeekday 。搭配组合规则同 E |
|
时钟 | h | 12小时制。可h/hh |
H | 24小时制。可H/HH |
|
分钟 | m | 分数。可m/mm |
秒钟 | s | 秒数。可s/ss |
经常使用的一些组合,大概是以上这些。按照Unicode Date Format
的话,其实还有季度、毫秒级的一些表示。ip
好比今日是 2018年1月16日。那么经过 DateFormatter 表示农历日期,能够是ci
let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "YYYY年MM月dd日 HH:mm:ss" dateFormatter.string(from: date) dateFormatter.locale = Locale(identifier: "zh_CN") dateFormatter.calendar = Calendar(identifier: .chinese) dateFormatter.dateStyle = .medium dateFormatter.dateFormat = "U年MMMd EEE a" dateFormatter.string(from: date) // 丁酉年冬月三十 周二 下午
其中,能够针对 DateFormatter 的 monthSymbols 等一些属性,设置咱们特有的一些表示习惯。好比,一月咱们但愿显示是正月,而冬月和腊月则显示十一月、十二月。那么能够经过这样进行设置。
let date = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "YYYY年MM月dd日 HH:mm:ss" dateFormatter.string(from: date) dateFormatter.locale = Locale(identifier: "zh_CN") dateFormatter.calendar = Calendar(identifier: .chinese) dateFormatter.dateStyle = .medium dateFormatter.shortMonthSymbols = ["正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"] dateFormatter.dateFormat = "U年MMMd EEE a" dateFormatter.string(from: date) // 丁酉年十一月三十 周二 下午
大概就这些了吧。