DateHelper
这是一个用于 iOS, watchOS, tvOS 和 macOS 的 Swift 日期扩展,它可以将日期创建为或从字符串中进行转换。它还可以比较、修改或提取日期组件,并使用缓存的格式化程序以获得性能优势。
功能
从字符串转换
date = Date(fromString: "2009-08-11", format: .isoDate)
转换为字符串
let mediumDateString = date.toString(style: .medium)
let rssDateString = date.toString(format: .rss)
let shortTimeString = date.toString(dateStyle: .none, timeStyle: .short)
let relativeTimeSting = date.toStringWithRelativeTime()
比较日期
let isToday = date.compare(.isToday)
let isSameWeek = date.compare(.isSameWeek(as: otherDate))
调整日期
let twoHoursBefore = date.adjust(.hour, offset: -2)
let atNoon = date.adjust(hour: 12, minute: 0, second: 0)
为 ... 创建日期
let startOfWeek = date.dateFor(.startOfWeek)
let nearest5Hours = date.dateFor(.nearestHour(hour:5))
强制一周从星期一开始
var calendar = Calendar(identifier: .gregorian)
calendar = 2 // sets the week to start on the second day.. monday
now.dateFor(.startOfWeek, calendar: calendar)
自 ... 以来时间
let secondsSince = date.since(otherDate, in: .second)
提取组件
let seconds = date.component(.second)
let daysInMonth = date.numberOfDaysInMonth()
let firstDayOfWeek = date.firstDayOfWeek()
let lastDayOfWeek = date.lastDayOfWeek()
用法
从字符串创建日期
使用初始化器 Date(fromString:String, format: DateFormatType)?
从字符串创建一个可选日期。
if let date = Date(fromString: "09 Sep 2011 15:26:08 +0200", format: .httpHeader) ->
{
// Do stuff with date
}
DateFormatType 枚举有几个预定义选项,以及一个用于提供自定义日期格式的元组。
case isoYear: i.e. 1997
case isoYearMonth: i.e. 1997-07
case isoDate: i.e. 1997-07-16
case isoDateTime: i.e. 1997-07-16T19:20+01:00
case isoDateTimeSec: i.e. 1997-07-16T19:20:30+01:00
case isoDateTimeMilliSec: i.e. 1997-07-16T19:20:30.45+01:00
case dotNet: i.e. "/Date(1268123281843)/"
case rss: i.e. "Fri, 09 Sep 2011 15:26:08 +0200"
case altRSS: i.e. "09 Sep 2011 15:26:08 +0200"
case httpHeader: i.e. "Tue, 15 Nov 1994 12:45:26 GMT"
case standard: "EEE MMM dd HH:mm:ss Z yyyy"
case custom(String): a custom date format string
将日期转换为字符串
有四种将日期转换为字符串的选项。
- date.toString(style: .shortWeekday)
- date.toString(format: .rss)
- date.toString(dateStyle: .short, timeStyle: .short)
- date.toStringWithRelativeTime()
1. 带样式的字符串
使用预定义的样式转换为字符串
let string = date.toString(.shortWeekday)
// Mon
DateStyleType 枚举
case short; "2/27/17, 2:22 PM"
case medium; "Feb 27, 2017, 2:22:06 PM"
case long; "February 27, 2017 at 2:22:06 PM EST"
case full; "Monday, February 27, 2017 at 2:22:06 PM Eastern Standard Time"
case ordinalDay; 27th
case weekday; "Monday"
case shortWeekday; "Mon"
case veryShortWeekday; "M"
case month; "February"
case shortMonth; "Feb"
case veryShortMonth; "F"
2. 带格式的字符串
使用特定或自定义的日期格式化程序转换为字符串。
let string = date.toString(format: .rss)
// Fri, 09 Sep 2011 15:26:08 +0200
DateFormatType 枚举有几个预定义选项,以及一个用于提供自定义日期格式的元组。
case isoYear: i.e. 1997
case isoYearMonth: i.e. 1997-07
case isoDate: i.e. 1997-07-16
case isoDateTime: i.e. 1997-07-16T19:20+01:00
case isoDateTimeSec: i.e. 1997-07-16T19:20:30+01:00
case isoDateTimeMilliSec: i.e. 1997-07-16T19:20:30.45+01:00
case dotNet: i.e. "/Date(1268123281843)/"
case rss: i.e. "Fri, 09 Sep 2011 15:26:08 +0200"
case altRSS: i.e. "09 Sep 2011 15:26:08 +0200"
case httpHeader: i.e. "Tue, 15 Nov 1994 12:45:26 GMT"
case standard: "EEE MMM dd HH:mm:ss Z yyyy"
case custom(String): a custom date format string
Unicode 日期字段符号指南
格式 | 描述 | 示例 |
---|---|---|
"y" | 1 位最小年份 | 1, 42, 2017 |
"yy" | 2 位年份 | 01, 42, 17 |
"yyy" | 3 位最小年份 | 001, 042, 2017 |
"yyyy" | 4 位最小年份 | 0001, 0042, 2017 |
"M" | 1 位最小月份 | 7, 12 |
"MM" | 2 位月份 | 07, 12 |
"MMM" | 3 个字母的月份缩写。 | Jul, Dec |
"MMMM" | 完整月份 | July, December |
"MMMMM" | 1 个字母的月份缩写。 | J, D |
"d" | 1 位最小日 | 4, 25 |
"dd" | 2 位日 | 04, 25 |
"E", "EE", "EEE" | 3 个字母的日子缩写。 | Wed, Thu |
"EEEE" | 完整的日子名称 | Wednesday, Thursday |
"EEEEE" | 1 个字母的日子缩写。 | W, T |
"EEEEEE" | 2 个字母的日子缩写。 | We, Th |
"a" | 一天中的时间段 | AM, PM |
"h" | AM/PM 1 位最小小时 | 5, 7 |
"hh" | AM/PM 2 位小时 | 05, 07 |
"H" | 24 小时制的 1 位最小小时 | 17, 7 |
"HH" | 24 小时制的 2 位小时 | 17, 07 |
"m" | 1 位最小分钟 | 1, 40 |
"mm" | 2 位分钟 | 01, 40 |
"s" | 1 位最小秒 | 1, 40 |
"ss" | 2 位秒 | 01, 40 |
"S" | 小数点后十分之一秒 | 123ms -> 1, 7ms -> 0 |
"SS" | 小数点后十分之一和百分之一秒 | 123ms -> 12, 7ms -> 00 |
"SSS" | 小数点后十分之一、百分之一和千分之一秒 | 123ms -> 123, 7ms -> 007 |
3. 使用日期和时间格式的字符串
使用DateFormatter.Style格式化日期和时间,并加上可选的isRelative标志进行转换。
let string = date.toString(dateStyle: .short, timeStyle: .short)
// 11/23/37 3:30 PM
DateFormatter.Style使用预定义的日期和时间格式。
case .none
case .short; "11/23/37" or "3:30 PM"
case .medium; "Nov 23, 1937" or "3:30:32 PM"
case .long; "November 23, 1937" or "3:30:32 PM PST"
case .full; "Tuesday, April 12, 1952 AD" or "3:30:42 PM Pacific Standard Time"
4. 相对时间格式的字符串
使用可定制的相对时间格式进行转换。
let string = date.toStringWithRelativeTime()
// yesterday
使用可选的strings:[RelativeTimeStringType:String]?
来自定义相对时间字符串
let string = date.toStringWithRelativeTime([.nowPast: "just posted"])
RelativeTimeStringType枚举的键用于自定义字符串
case nowPast: "just now"
case nowFuture: "in a few seconds"
case secondsPast: "%.f seconds ago"
case secondsFuture: "in %.f seconds"
case oneMinutePast: "1 minute ago"
case oneMinuteFuture: "in 1 minute"
case minutesPast: "%.f minutes ago"
case minutesFuture: "in %.f minutes"
case oneHourPast: "last hour"
case oneHourFuture: "next hour"
case hoursPast: "%.f hours ago"
case hoursFuture: "in %.f hours"
case oneDayPast: "yesterday"
case oneDayFuture: "tomorrow"
case daysPast: "%.f days ago"
case daysFuture: "in %.f days"
case oneWeekPast: "last week"
case oneWeekFuture: "next week"
case weeksPast: "%.f weeks ago"
case weeksFuture: "in %.f weeks"
case oneMonthPast: "last month"
case oneMonthFuture: "next month"
case monthsPast: "%.f months ago"
case monthsFuture: "in %.f months"
case oneYearPast: "last year"
case oneYearFuture: "next year"
case yearsPast: "%.f years ago"
case yearsFuture: "in %.f years"
比较日期
使用compare(_ comparison:DateComparisonType) -> Bool
函数对日期与类型或日期进行比较。
var isSameDay = now.compare(.isSameDay(as: date))
var isToday = now.compare(.isToday)
DateComparisonType枚举可以用于比较日期与预定义日期、周期或自定义日期(元组)
// Days
case isToday
case isTomorrow
case isYesterday
case isSameDay(as:Date)
// Weeks
case isThisWeek
case isNextWeek
case isLastWeek
case isSameWeek(as:Date)
// Months
case isThisMonth
case isNextMonth
case isLastMonth
case isSameMonth(as:Date)
// Years
case isThisYear
case isNextYear
case isLastYear
case isSameYear(as:Date)
// Relative Time
case isInTheFuture
case isInThePast
case isEarlier(than:Date)
case isLater(than:Date)
case isWeekday
case isWeekend
调整日期
使用adjust(_ component:DateComponentType, offset:Int) -> Date
函数通过偏移量更改日期。
date = now.adjust(.day, offset: -4)
DateComponentType枚举用于可调整的组件。
case second
case minute
case hour
case day
case weekday
case nthWeekday
case week
case month
case year
使用adjust(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date
函数更改日期组件。
let atNoon = date.adjust(hour: 12, minute: 0, second: 0)
为...创建日期
使用dateFor(_ type:DateForType) -> Date
函数为相对于日期的特定事件创建新的日期。
let date = now.dateFor(.startOfWeek)
let nearestHour = now.dateFor(.nearestHour(hour:1))
DateForType枚举用于预定选项列表。
case startOfDay
case endOfDay
case startOfWeek
case endOfWeek
case startOfMonth
case endOfMonth
case tomorrow
case yesterday
case nearestHour
case nearestMinute(minute:Int)
case nearestHour(hour:Int)
时间自...
使用 since(_ date:Date, in component:DateComponentType) -> Int64
函数来获取与另一日期的比较时间组件值。
var num = date.since(now, in: .second)
The DateComponentType 枚举用于获取的值。
case second
case minute
case hour
case day
case weekday
case nthWeekday
case week
case month
case year
提取组件
使用 component(_ component:DateComponentType) -> Int?
从日期中获取特定的组件。
let minute = date.component(.minute)
The DateComponentType 枚举用于获取的值。
case second
case minute
case hour
case day
case weekday
case nthWeekday
case week
case month
case year
与日期相关的组件
date.numberOfDaysInMonth()
date.firstDayOfWeek()
date.lastDayofWeek()
要求
语言:Swift 5.0 最小:iOS 11,tvOS 12,watchOS 4,macOS 10.14
安装
Carthage github "melvitax/DateHelper"
Swift Package Manager https://github.com/melvitax/DateHelper.git
Cocoapods pod 'AFDateHelper', '~> 4.3.0'
手动 在您的项目中包含 DateHelper.swift
作者
Melvin Rivera
许可
DateHelper 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。