DateComponentsFormatter-相同的日期,不同的结果

发布于 2025-01-30 17:25:19 字数 876 浏览 3 评论 0原文

为什么detteremainingtext2给出了dateremainingtext的不同结果?
显然,detheremainingtext2是错误的。

这是我的代码:

import Foundation

let startDate = Calendar.current.date(from: DateComponents(year: 2022, month: 5, day: 1)) ?? .now
let endDate = Calendar.current.date(from: DateComponents(year: 2020, month: 6, day: 2)) ?? .now

let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.year, .month, .day]
dateComponentsFormatter.unitsStyle = .full
var dateRemainingText = dateComponentsFormatter.string(from: startDate, to: endDate)! // -1 year, 10 months, 29 days
let dateComponents = Calendar.current.dateComponents([.year, .month, .day], from: startDate, to: endDate)
var dateRemainingText2 = dateComponentsFormatter.string(from: dateComponents) // -1 year, 11 months, 1 day

Why is dateRemainingText2 giving different result from dateRemainingText?

Obviously dateRemainingText2 is wrong.

Here's my code:

import Foundation

let startDate = Calendar.current.date(from: DateComponents(year: 2022, month: 5, day: 1)) ?? .now
let endDate = Calendar.current.date(from: DateComponents(year: 2020, month: 6, day: 2)) ?? .now

let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.year, .month, .day]
dateComponentsFormatter.unitsStyle = .full
var dateRemainingText = dateComponentsFormatter.string(from: startDate, to: endDate)! // -1 year, 10 months, 29 days
let dateComponents = Calendar.current.dateComponents([.year, .month, .day], from: startDate, to: endDate)
var dateRemainingText2 = dateComponentsFormatter.string(from: dateComponents) // -1 year, 11 months, 1 day

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2025-02-06 17:25:19

datecomponentsformatter.string(来自:to:) and datecomponentsformatter.string(来自:)是不同的方法,因此它们可以做不同的事情。

从某些实验中,我们可以看到String(来自:)输出一个字符串,该字符串描述了dateComponent s中所有日期组件的 sum 传递。

这只是发生的string的输出相同(从:to:)对于大多数组件都是正面的情况。

示例:

// 1 month + 7 days
var dateComponents = DateComponents()
dateComponents.month = 1
dateComponents.day = 7
// 1 month, 7 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// 7 days - 1 month
dateComponents.month = -1
dateComponents.day = 7
// -24 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// 1 month - 7 days
dateComponents.month = 1
dateComponents.day = -7
// 24 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// 1 month + 30 days
// note that adding 1 month, it's February, and there are only 28 days
dateComponents.month = 1
dateComponents.day = 30
// 2 months, 2 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// -10 months - 30 days
// note that after subtracting 10 months, it is March of the previous year, 
// which also happens to be a leap year, so February has 29 days
// Subtracting 30 days from that will bring us to January, with one day left
// which is, where the extra month and day came from
dateComponents.month = -10
dateComponents.day = -30
// -11 months, 1 day
print(dateComponentsFormatter.string(from: dateComponents)!)

// -1 year - 10 months - 30 days
// similar to above, except not a leap year
// so we have 2 days left after subtracting from a 28-day February.
dateComponents.year = -1
dateComponents.month = -10
dateComponents.day = -30
// -1 year, 11 months, 2 days
print(dateComponentsFormatter.string(from: dateComponents)!)

DateComponents在最后情况中使用的是calendar.datecomponents(_:to:to:)在您的代码中。

另一方面,字符串(从:到)是格式化两个日期之间的指定方法。

DateComponentsFormatter.string(from:to:) and DateComponentsFormatter.string(from:) are different methods and so they can do different things.

From some experimentation, we can see that string(from:) outputs a string that describes the sum of all the date components in the DateComponents passed in.

This just happens to be the same as the output for string(from:to:) for most of the cases where the components are all positive.

Examples:

// 1 month + 7 days
var dateComponents = DateComponents()
dateComponents.month = 1
dateComponents.day = 7
// 1 month, 7 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// 7 days - 1 month
dateComponents.month = -1
dateComponents.day = 7
// -24 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// 1 month - 7 days
dateComponents.month = 1
dateComponents.day = -7
// 24 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// 1 month + 30 days
// note that adding 1 month, it's February, and there are only 28 days
dateComponents.month = 1
dateComponents.day = 30
// 2 months, 2 days
print(dateComponentsFormatter.string(from: dateComponents)!)

// -10 months - 30 days
// note that after subtracting 10 months, it is March of the previous year, 
// which also happens to be a leap year, so February has 29 days
// Subtracting 30 days from that will bring us to January, with one day left
// which is, where the extra month and day came from
dateComponents.month = -10
dateComponents.day = -30
// -11 months, 1 day
print(dateComponentsFormatter.string(from: dateComponents)!)

// -1 year - 10 months - 30 days
// similar to above, except not a leap year
// so we have 2 days left after subtracting from a 28-day February.
dateComponents.year = -1
dateComponents.month = -10
dateComponents.day = -30
// -1 year, 11 months, 2 days
print(dateComponentsFormatter.string(from: dateComponents)!)

The DateComponents used in that last case is what Calendar.dateComponents(_:from:to:) in your code.

On the other hand, string(from:to) is the designated method to format the period between two dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文