[英]Adjusting Swift Date from one timezone to another is not producing expected result
我真的在為日期和時區而苦苦掙扎。 我的理解是Date與時區無關,只是一個時間點。 我正在編寫一個單元測試,我想創建一個Date object,如果用戶所在的時區與我正在運行測試的默認倫敦區不同,那么它代表現在的時間,然后將其轉換為不同的時區。 我首先使用帶有預計時間的DateComponents創建 ...
[英]Adjusting Swift Date from one timezone to another is not producing expected result
我真的在為日期和時區而苦苦掙扎。 我的理解是Date與時區無關,只是一個時間點。 我正在編寫一個單元測試,我想創建一個Date object,如果用戶所在的時區與我正在運行測試的默認倫敦區不同,那么它代表現在的時間,然后將其轉換為不同的時區。 我首先使用帶有預計時間的DateComponents創建 ...
[英]Predicate for fetching the data for a particular year from Core Data in SwiftUI
我正在開發一個 SwiftUI 預算管理應用程序,用於跟蹤用戶的支出和收入。 該應用程序使用 CoreData 來管理數據管理。 托管 object 的日期是一個財產。 我使用下面的獲取請求來獲取數據 這將提供分組數據,例如 2021 年 9 月的費用管理的object model如下 我想根據年份 ...
[英]How to generate an ordered list of DateComponents.weekday values for the current locale
來自DateComponents.weekday的文檔工作日單位是從 1 到 n 的數字,其中 n 是一周中的天數。 例如,在公歷中,n 為 7,星期日用 1 表示。 從Calendar.firstWeekday firstWeekday 的默認值因日歷和區域設置而異...對於公歷和 ISO 86 ...
[英]Initialising DateComponents with weekOfMonth returning wrong date
這是代碼: 這是 output: September 1, 2022 但正確答案應該是September 11, 2022 。 為什么? 這是一個錯誤還是我只是錯過了什么? ...
[英]dateComponents() from... to in days shows from now to tomorrow as 0 days?
我有代表未來某個時間的 datecomponent 對象。 我想計算從現在到那個日期有多少個日期。 我還簡單地將日期表示為日期。 我發現,當我試圖顯示“明天”的日期有多少時,它顯示為 0。在我看來,它應該顯示為 1。我可以嘗試一種 hacky 方法,將 1 添加到我的計數,但我想知道是因為它試圖四 ...
[英]How to Convert NSDateComponents to String?
<NSDateComponents: 0x282884930> { 日歷年: 2022 月: 7 天: 12 小時: 12 分鍾: 23 如何將其轉換為更易讀的格式:“2022 年 7 月 12 日,上午 12:23”? ...
[英]How to format a time interval with the new Foundation formatted method?
我正在嘗試將DateComponentsFormatter與新的 Foundation 格式化程序一起使用。 要格式化日期,我可以這樣做: 但是,我正在嘗試使用這個新 API 來使用DateComponentsFormatter : 如何使用DateComponentsFormatter的新 ...
[英]How to convert DateComponent 'day' to Int for getting average of Array of durations in days, or way to get an avg of DateComponents?
我有一個 DateComponents 數組,每個都像這樣生成: Calendar.current.dateComponents([.day], from: lastPeriodStart!, to: Date()) 打印時的示例是: day: -3 isLeapMonth: false 基本上 ...
[英]Problem with adding month value to date in Swift
我有問題。 我正在獲取當前年份的開始日期,功能如下: 我的目標是遍歷這個日期以提取每個月的數據。 我使用此函數將值添加到 .month 以獲取一年中每個月的開始日期: 當我遍歷此功能並打印日期時,我收到以下信息: 輸出: 如您所見,從 2022-03-31 開始存在問題 - 它應該返 ...
[英]DateComponentsFormatter - same dates, different results
為什么dateRemainingText2給出的結果與dateRemainingText不同? 顯然dateRemainingText2是錯誤的。 這是我的代碼: ...
[英]iOS diff in days using dateComponent(_:from:to:) - what does nil mean for the result day?
我正在計算兩個Date之間的差異: let diff = Calendar.current.dateComponents([.day], from: oldDate, to newDate) 結果我得到了一個DateComponents ,其中day屬性是可選的,文檔並沒有說在這個計算中保證有一 ...
[英]DateComponents doesn't seem to honor year
我正在嘗試將Date轉換為DateComponents ,更改一些屬性,並獲取新的Date 。 我編寫了以下代碼: 我已經在 iOS 15.0 上的 iOS 模擬器以及運行最新 macOS Big Sur 和 Xcode 13.0 的 Mac 上的 Swift REPL 上在America/De ...
[英]How to format DateComponents as a partial date string?
我有一個DateComponents對象,它包含一個月份和一個年份,我想從中獲得一個格式為“2021 年 3 月”的格式化字符串。 我已經嘗試過DateComponentsFormatter但沒有一個屬性接近我想要看到的。 let components = DateComponents(calend ...
[英]Why local notification is not firing for UNCalendarNotificationTrigger
本地通知應該在 25-9-2021 觸發。 這是火災時間的打印 object ▿ 年:2021 月:9 日:26 isLeapMonth: false 年份:2021 月:9 天:25 是閏月:假let content = UNMutableNotificationContent() conte ...
[英]dateComponents(_, from, to) returning wrong data until Picker touched
我有 2 個 DatePickers,用戶可以在其中 select 小時和分鍾。 一旦選擇器被更改, “calcDuration”函數就會被調用,該函數應該計算時間差,並以小時和分鍾的形式將其顯示在 label 上。 基本 storyboard 設置 如果我運行應用程序並將較低 datePicker ...
[英]Schedule local notification every n days (timezone safe)
我相信這個問題已經被問過好幾次了,但沒有一個明確的答案。 有兩種安排時間通知的方法: UNCalendarNotification和UNTimeIntervalNotificationTrigger 。 在一周中的特定時間和日期安排通知是微不足道的,與在一個月的特定日期相同,但在特定時間安排通知, ...
[英]Date for titleForHeaderInSection with FetchedResultsController
我有一個屬性@NSManaged public var sectionKeyDate: Date? 我正在嘗試使用sectionKey 。 我在從NSFetchedResultsSectionInfo獲取結果字符串以格式化為Date ,然后再轉換回格式化字符串時遇到問題。 從字符串到日期的轉換失敗並 ...
[英]Swift date formatter ignoring months
我寫了一個日期重新格式化程序,但似乎 Swift 的日期格式化程序本身忽略了月份。 文檔說這不應該發生。 我如何讓它不忽略幾個月? 這打印: 它不合理地把所有月份都變成了 1! ...
[英]Swift date difference in nanoseconds is not working
我正在實現一個計時器,該計時器需要計算用戶處於非活動狀態的毫秒數並計算差異並恢復計時器。 由於 dateComponents 中沒有毫秒選項,因此我使用了納秒,但是,當我嘗試計算兩個日期之間的納秒間隔時,每次都會得到相同的結果(當前日期正在改變,應該得到不同的結果),如果我改變納秒到秒,它的工作原理 ...
[英]Format hour and minute integers as the String “9:30 am”
如何將小時和分鍾 integer 值表示為格式為“9:30 am”的字符串? 目前,我有: 我知道我可以在最后手動連接時間子午線,但我希望有一個內置的 function 用於此。 也許是不同的UnitsStyle ? ...