如何在 NSDate 中新增一天?


在本文中,您將學習如何在 Swift 語言中向日期新增天數。

很多時候,您可能需要透過更改幾天、幾周、幾個月等來修改日期。Swift 提供了一個內建的概念來向日期新增元件。讓我們看一些例子。

讓我們簡要了解一下這些示例中將使用的一些常見概念。

日曆類

Swift 中的 Calendar 類是一個表示日曆的類,日曆是組織日期的系統。它用於執行與日曆相關的計算,例如確定一個月的天數或一週的第一天。

日期格式化程式類

Swift 中的 DateFormatter 類是一個用於在日期和字串之間進行轉換的類。它可以用於從字串解析日期或將日期格式化為字串。

如何向自定義日期新增一天?

示例 1

演算法

  • 步驟 1 - 獲取當前日曆引用物件

  • 步驟 2 - 建立 DateFormatter 類的物件

  • 步驟 3 - 設定時區

  • 步驟 4 - 建立輸入日期字串

  • 步驟 5 - 根據日期字串設定日期格式

  • 步驟 6 - 將日期字串轉換為日期物件

  • 步驟 7 - 使用天數的值新增日期元件

示例

import Foundation
let calendar = Calendar.current
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .init(abbreviation: "UTC")
let dateString1 = "08-10-2022"
dateFormatter.dateFormat = "dd-MM-yyyy"
if let dateObject = dateFormatter.date(from: dateString1),
   let dateByAddedDay = calendar.date(byAdding: .day, value: 1, to: dateObject) {
    print("the target date: \(dateObject) and date after adding one day: \(dateByAddedDay)")
}

輸出

the target date: 2022-10-08 00:00:00 +0000 and date after adding one day: 2022-10-09 00:00:00 +0000

示例 2

import Foundation
let calendar = Calendar.current
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .init(abbreviation: "UTC")
let dateString2 = "12/23/2022"
dateFormatter.dateFormat = "MM/dd/yyyy"
if let dateObject = dateFormatter.date(from: dateString2),
   let dateByAddedDay = calendar.date(byAdding: .day, value: 1, to: dateObject) {
    print("the target date: \(dateObject) and date after adding one day: \(dateByAddedDay)")
}

輸出

the target date: 2022-12-23 00:00:00 +0000 and date after adding one day: 2022-12-24 00:00:00 +0000

說明

我們在上面的示例中更改了日期格式,然後添加了天數。

如何向當前日期新增一天?

import Foundation
let calendar = Calendar.current
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .init(abbreviation: "UTC")
let targetDate = Date()
if let dateByAddedDay = calendar.date(byAdding: .day, value: 1, to: targetDate) {
    print("the target date: \(targetDate) and date after adding one day: \(dateByAddedDay)")
}

輸出

the target date: 2023-01-06 16:49:29 +0000 and date after adding one day: 2023-01-07 16:49:29 +0000

請注意,輸出可能因您學習此內容的日期而異。

如何向日期新增其他元件?

同樣,您可以新增其他日期元件,例如周、月、年等。

示例

import Foundation
let calendar = Calendar.current
let dateFormatter = DateFormatter()
dateFormatter.timeZone = .init(abbreviation: "UTC")
let dateString = "08-10-2022"
dateFormatter.dateFormat = "dd-MM-yyyy"
if let dateObject = dateFormatter.date(from: dateString) {
    
    print("The target date: \(dateObject)")
    
    if let dateByAddedDays = calendar.date(byAdding: .day, value: 7, to: dateObject) {
        print("Next Week: \(dateByAddedDays)")
    }
    
    if let dateByAddedMonth = calendar.date(byAdding: .month, value: 1, to: dateObject) {
        print("Next Month: \(dateByAddedMonth)")
    }
    
    if let dateByAddedYear = calendar.date(byAdding: .year, value: 1, to: dateObject) {
        print("Next Year: \(dateByAddedYear)")
    }
}

輸出

The target date: 2022-10-08 00:00:00 +0000
Next Week: 2022-10-15 00:00:00 +0000
Next Month: 2022-11-08 00:00:00 +0000
Next Year: 2023-10-08 00:00:00 +0000

說明

在上面的示例中,您轉換了日期以獲取下一週、下一月和下一年。

結論

Calendar 和 DateFormatter 類可用於透過新增不同的日期元件來操作日期。您可以修改自定義日期和當前日期。

更新於:2023年9月7日

201 次檢視

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告