ios - 如何在 Calendar.current.isDateInToday 中考虑时区?
问题描述
我使用返回日期字符串的 API,例如2021-05-05T23:00:00+00:00
. 然后我将其转换为 Swift Date 对象,并尝试使用 Calendar.current 检查日期是今天还是明天。
let apiDate = "2021-05-05T23:00:00+00:00";
let dateFormatter = ISO8601DateFormatter()
let myDate = dateFormatter.date(from: apiDate)!
if Calendar.current.isDateInToday(myDate) {
print("Date is today") // this should be true
} else if Calendar.current.isDateInTomorrow(myDate) {
print("Date is tomorrow") // this is true instead
} else {
print("Date is not today or tomorrow")
}
问题是isDateInToday
应该是true
。出于某种原因,它在检查是否是今天并且返回时添加了我的时区(+3 小时)false
。我怎样才能使我得到基于电话当地时间的结果?
解决方案
推荐阅读
- javascript - 显示动态生成的输入字段的输入值
- python - 尝试在python中使用循环进行矩阵向量和矩阵矩阵乘法
- reactjs - 如何从 1 个组件中的两个端点获取数据?
- python - Python定义具有其他变量类型的变量
- java - 你如何在java中四舍五入一个双用户输入值?
- python - 在 Python 中删除二叉树节点。为什么 self != parent.left 或 parent.right?
- powerbi - 为什么平均值不适用于 power bi dax
- visual-studio-code - 有没有办法摆脱 VS 代码中浏览器评论扩展的模糊预览?
- c++ - 如何使用自定义错误消息使概念失败 (C++20)
- mdx - 使用日期小于的 MDX 获取数据