- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
下面的示例代码从当前 Date 获取 DateComponents,修改组件,并从修改后的组件创建一个新的 Date。它还展示了创建一个新的 DateComponents 对象,填充它,然后从中创建一个新的 Date。
import Foundation
let utcHourOffset = -7.0
let tz = TimeZone(secondsFromGMT: Int(utcHourOffset*60.0*60.0))!
let calendar = Calendar(identifier: .gregorian)
var now = calendar.dateComponents(in: tz, from: Date())
// Get and display current date
print("\nCurrent Date:")
print("\(now.month!)/\(now.day!)/\(now.year!) \(now.hour!):\(now.minute!):\(now.second!) \(now.timeZone!)")
let curDate = calendar.date(from: now)
print("\(curDate!)")
// Modify and display current date
now.year = 2010
now.month = 2
now.day = 24
now.minute = 0
print("\nModified Date:")
print("\(now.month!)/\(now.day!)/\(now.year!) \(now.hour!):\(now.minute!):\(now.second!) \(now.timeZone!)")
let modDate = calendar.date(from: now)
print("\(modDate!)")
// Create completely new date
var dc = DateComponents()
dc.year = 2014
dc.month = 12
dc.day = 25
dc.hour = 10
dc.minute = 12
dc.second = 34
print("\nNew Date:")
print("\(dc.month!)/\(dc.day!)/\(dc.year!) \(dc.hour!):\(dc.minute!):\(dc.second!) \(now.timeZone!)")
let newDate = calendar.date(from: dc)
print("\(newDate!)")
如果我修改组件,设置不同的年、月、日等,然后使用组件获取日期,我得到了意想不到的结果,新日期包含除年之外的所有修改组件,保持不变。
在我创建一个 DateComponents 对象并填充它然后从中创建一个 Date 的情况下,它按预期工作。
代码的输出如下所示:
Current Date:
3/9/2017 19:5:30 GMT-0700 (fixed)
2017-03-10 02:05:30 +0000
Modified Date:
2/24/2010 19:0:30 GMT-0700 (fixed)
2017-02-25 02:00:30 +0000
New Date:
12/25/2014 10:12:34 GMT-0700 (fixed)
2014-12-25 17:12:34 +0000
我预计修改后的日期是 2010-02-25 02:00:30 +0000
而不是 2017-02-25 02:00:30 +0000
.为什么不是呢?为什么它在第二种情况下有效?
docs对于 DateComponents 说:“NSDateComponents 的实例不负责回答有关超出其初始化信息的日期的问题......”。由于 DateComponents 对象是用一年初始化的,所以这似乎不适用,但这是我在文档中看到的唯一可以解释我观察到的行为的东西。
最佳答案
如果您登录 now
和 dc
,您将看到问题所在。 now
是从 Date
创建的。这将填充所有日期组件,包括 yearForWeekOfYear
和几个与工作日相关的组件。这些组件导致 modDate
出现错误。
newDate
按预期工作,因为只设置了特定组件。
如果您重置一些额外的组件,您可以使 modDate
正确输出。具体来说,添加:
now.yearForWeekOfYear = nil
就在创建 modDate
之前将产生 modDate
的预期日期。当然,最好的解决方案是创建一个新的 DateComponents
实例,并根据需要使用以前的 DateComponents
中的特定值:
let mod = DateComponents()
mod.timeZone = now.timeZone
mod.year = 2010
mod.month = 2
mod.day = 24
mod.hour = now.hour
mod.minute = 0
mod.second = now.second
print("\nModified Date:")
print("\(mod.month!)/\(mod.day!)/\(mod.year!) \(mod.hour!):\(mod.minute!):\(mod.second!) \(mod.timeZone!)")
let modDate = calendar.date(from: mod)
print("\(modDate!)")
关于Swift:设置 DateComponents 年份时的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708963/
我正在尝试将 Date 转换为 DateComponents,更改一些属性,然后获取新的 Date。我写了以下代码: import Foundation var components = Calend
我正在尝试使用 DateComponents 转换今天的日期: let calendar = Calendar.current //add today's date var todayD
我制作了一个待办事项列表应用程序。在应用程序中,用户可以选择 4 个按钮之一来设置通知。立即,上午,下午和晚上。目前,晚上和即时工作但早上和下午不工作,我不确定为什么。 这是我晚上的代码: @IBAc
我正在开发一个显示当前月份的日历 View (Cocoa macOS 应用程序)。当我尝试计算给定日期的周数时,我会根据日历和区域设置得到不同的结果。 对于 2021 年 1 月 1 日,某些调用返回
我正在开发一个显示当前月份的日历 View (Cocoa macOS 应用程序)。当我尝试计算给定日期的周数时,我会根据日历和区域设置得到不同的结果。 对于 2021 年 1 月 1 日,某些调用返回
我有一个创建日期的函数... func date(withYear year: Int, month: Int, day: Int) -> Date { let components = Da
我可以得到两个特定日期之间的差异,这将等于日期总数。现在我想将工作日除以总天数并得到整数输出。 @IBAction func go(_ sender: UIButton) { let con
我想得到一周的第一天和最后一天。但我的结果与苹果的文档不符: https://developer.apple.com/documentation/foundation/nsdatecomponents
下面的示例代码从当前 Date 获取 DateComponents,修改组件,并从修改后的组件创建一个新的 Date。它还展示了创建一个新的 DateComponents 对象,填充它,然后从中创建一
尝试在 swift 中使用日期组件时,日期之间的较大间隔会崩溃(例如,使用的日期是 1979 年 8 月 16 日至 2053 年 5 月 11 日)日期组件的代码是 ComponentsforDet
我正在尝试从 API 端点的日期值中获取日期组件。我需要保留日期值,因为它们已经针对时区进行了调整。不幸的是,我无法控制 API 如何返回日期值。 当我从 API 获取日期值 (sessionTime
由于某些奇怪的原因,在执行 HKActivitySummaryQuery 时,每个摘要的返回日期组件都晚了一天。查询返回正确日期的数据,但数据的 dateComponents 日期晚了一天。我已尝试设
我在这行代码上发生了崩溃,但我不完全确定原因。 let difference = calendar.dateComponents([.day, .hour, .minute], from: start
为了获得两个日期之间的天数差异,我执行了以下操作: let calendar = Calendar.current guard let startDate = timeStartDate else {
这个问题在这里已经有了答案: Calculate age from birth date (4 个答案) 关闭 3 年前。 我正在尝试编写一个代码,根据分配给文本字段的日期选择器计算年龄。我得到正确
鉴于我在扩展中添加到 Date 对象的以下方法: extension Date { static func -(left: Date, right: Date) -> DateComponen
我正在制作一个单 View 应用程序,它管理一个人处理的所有任务。我有两个数组:一个存储所有任务名称,一个显示任务的完成时间。我将这两个分配给 UserDefaults: var tasks = [S
我为 NSDate 创建了一个扩展,它删除了时间组件,以允许仅基于日期对 NSDate 进行相等性检查。我通过获取原始 NSDate 对象,使用 DateComponent 类获取日、月和年,然后使用
我正在计算两个日期之间的时差。我有一段代码: func timeAgoSinceDate(_ date:Date, numericDates:Bool) -> String { let cal
在我的应用程序中,我有一个联系人列表,包括联系人的生日。现在我想看看我的哪些联系人在接下来的 7 天内过生日。我在列表中使用过滤器来过滤并仅返回匹配的联系人 let timeSpan = 7
我是一名优秀的程序员,十分优秀!