swift - 获取特定时区的当前日期
问题描述
我正在尝试在用户选择的时区中获取当前日期,但是尽管使用了 stackoverflow 上其他答案中的函数,但除了当前日期之外,我什么也得不到。我用的是这个:
func dateIn(timeZone: TimeZone) -> Date {
let now = Date()
var calendar = Calendar.current
calendar.timeZone = timeZone
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
let date = calendar.date(from: components) ?? Date()
return date
}
如果有人能帮助我理解为什么我没有在时区获得日期,我将非常感激,尽管我将日历的时区更改为该时区。
解决方案
推荐阅读
- javascript - 带有箭头和匿名函数的 this 关键字
- eclipse - 如何将引用的库与我的项目一起发送
- django - 如何在 Django 中使用 None 保存 FileField?
- solr - 索尔 | 高亮返回所有不匹配的字段
- python - 在 Flask 中将大型查询结果转换为模板的最佳方法
- android - 修改 videoBuffer 以从 DJI 无人机流式传输到 RTSP
- react-native - 在 react native 中使用 setState 获取更新的值
- c++ - c ++为函数表建立名称,函数具有不同的签名
- python - Pythonic 编写函数的方式
- numpy - Numpy`where`子句的奇怪行为