我所面臨的問題,爲單位時DateComponentsFormatter
返回意外數字。有沒有人遇到同樣的問題?DateComponentsFormatter返回單位的錯誤數計數
import Foundation
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full;
formatter.maximumUnitCount = 1;
let date = Date(timeIntervalSinceNow: -14.7 * 24 * 60 * 60)
let dateString = formatter.string(from: date, to: Date()) // 2 weeks 1 day
我預計會收到「2周」,但有「2周1天」。
你爲什麼使用'14.7'? – shallowThought
@shallowThought我已經設置了'maximumUnitCount' - 格式化程序不應該返回多個單元。 –
僅供參考 - 使用'日期(timeIntervalSinceNow:-14.7 * 24 * 60 * 60)'是做日期數學的可怕方法。不是每天都有24小時。不是每個小時都有3600秒。使用'日期日期(byAdding ...)'方法之一。 – rmaddy