Swift Date时间
Swift Date时间

Swift Date时间

Date 是用于处理时间和日期的核心类,它提供了多种操作时间的便捷方法,包括创建时间、计算时间间隔和格式化日期等,属于Foundation库。

Date 表示一个绝对时间点(自参考时间 2001-01-01 00:00:00 UTC 以来的秒数),不包含时区或日历语义。它是轻量的时间点值类型。

初始化方法

1、创建当前时间的 Date 对象

let now = Date()        // 当前时间
let now = Date.now        // 当前时间

2、自定义初始化

使用 DateComponents 和 Calendar,创建自定义日期:

let components = DateComponents(year:2024, month:12, day:25)
Calendar.current.date(from: components)	// 可选值,输出 2024-12-25 12:00:00

3、基于UNIX时间戳初始化

UNIX时间戳以秒为单位,创建日期:

let timestamp: TimeInterval = 1710028800
Date(timeIntervalSince1970: timestamp)	// Mar 10, 2024 

4、基于“参考时间点”初始化

从 Apple 的参考时间(2001-01-01 00:00:00 UTC)初始化:

Date(timeIntervalSinceReferenceDate: 0) // Jan 1, 2001
Date(timeIntervalSinceReferenceDate: 86400) // Jan 2, 2001

5、基于“时间间隔”初始化

时间间隔为负数表示之前的时间点,时间间隔为正数表示之后的时间点:

Date(timeInterval: -3600, since: Date())  // 当前时间前一小时
Date(timeInterval: 3600, since: Date())  // 当前时间后一小时
Date(timeIntervalSinceNow: 3600)    // 当前时间后一小时

常用属性

1、Date.distantPast

非常遥远的过去,例如:0001-01-01 00:00:00 +0000。

let farPast = Date.distantPast  // 极早的时间
print("farPast:\(farPast)")     // farPast:0001-01-01 00:00:00 +0000

2、Date.distantFuture

非常遥远的未来,例如:4001-01-01 00:00:00 +0000。

let farFuture = Date.distantFuture // 极晚的时间
print("farFuture:\(farFuture)")     //  farFuture:4001-01-01 00:00:00 +0000

3、timeIntervalSinceNow

返回当前日期与未来或过去某个时间点之间的秒数。

let futureDate = Date().addingTimeInterval(3600)
let interval = futureDate.timeIntervalSinceNow // 返回 3600 秒或 3599近似的秒数

4、timeIntervalSince1970

返回从 Unix 纪元时间(1970-01-01 00:00:00 UTC)到当前时间的秒数。

let timestamp = Date().timeIntervalSince1970 // 当前时间的 Unix 时间戳

5、timeIntervalSinceReferenceDate

返回从 Apple 的参考时间(2001-01-01 00:00:00 UTC)到当前时间的秒数。

let secondsSince2001 = Date().timeIntervalSinceReferenceDate

常用方法

1、addingTimeInterval(_:)

用于在当前 Date 对象的基础上增加或减少秒数。

let now = Date()
let futureDate = now.addingTimeInterval(3600) // 当前时间加 3600 秒(1 小时)
let pastDate = now.addingTimeInterval(-86400) // 当前时间减 86400 秒(1 天)

2、timeIntervalSince(_:)

返回当前日期与给定日期之间的时间间隔(以秒为单位)。

let now = Date()
let anotherDate = now.addingTimeInterval(-3600)
let interval = now.timeIntervalSince(anotherDate) // 返回 3600 秒

3、compare(_:)

比较两个日期,返回 ComparisonResult 枚举值:

.orderedAscending:当前日期早于给定日期。

.orderedDescending:当前日期晚于给定日期。

.orderedSame:两个日期相同。

let now = Date()
let futureDate = now.addingTimeInterval(3600)
let result = now.compare(futureDate) // 返回 .orderedAscending

4、distance(to:)

返回给定日期与当前日期之间的时间间隔:

let now = Date()
let anotherDate = now.addingTimeInterval(-3600)
let interval = now.distance(to: anotherDate) // 返回 -3600 秒

注:timeIntervalSince为当前日期 – 给定日期,distance为给定日期 – 当前日期,两者相反。

运算符支持

1、比较Date

let date1 = Date()
let date2 = date1.addingTimeInterval(3600)

date1 < date2  // true
date1 == date2 // false

2、+和-运算符

let newDate = date1 + 3600  // 1 小时后
let diff = date2 - date1    // 返回 TimeInterval

日期格式化

1、description

将日期转为默认字符串(调试用)。

let now = Date()
print(now.description) // 例如:2024-12-14 08:00:00 +0000

2、DateFormatter格式化日期

使用 DateFormatter 格式化日期:

let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .short

let now = Date()
let formattedDate = formatter.string(from: now)
print(formattedDate) // 例如:Dec 14, 2024 at 8:00 AM

3、formatted格式化日期

使用Date.FormatStyle格式化日期:

let now = Date()
let text = now.formatted(Date.FormatStyle().year().month().day())   // Dec, 8, 2025

提取日期和时间

通过 Calendar 提取日期的特定部分(年、月、日、小时等):

let now = Date()
let calendar = Calendar.current

let year = calendar.component(.year, from: now)
let month = calendar.component(.month, from: now)
let day = calendar.component(.day, from: now)

print("\(year)-\(month)-\(day)")	// 2025-12-8

总结

Swift使用Date表示时间,可以通过多种形式进行初始化,配合DateComponents 和 Calendar,自定义时间。

相关文章

1、SwiftUI Date格式化formatted:https://fangjunyu.com/2025/01/14/swift%e6%a0%bc%e5%bc%8f%e5%8c%96%e6%97%a5%e6%9c%9f%e5%92%8c%e6%97%b6%e9%97%b4%e7%9a%84date-formatstyle/

2、SwiftUI DateFormatter日期格式化:https://fangjunyu.com/2024/10/06/swift-ui%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3dateformatter/

   

如果您认为这篇文章给您带来了帮助,您可以在此通过支付宝或者微信打赏网站开发者。

欢迎加入我们的 微信交流群QQ交流群,交流更多精彩内容!
微信交流群二维码 QQ交流群二维码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注