ios - 在 swift 中使用组件日期时获取递增的日期?

标签 ios swift xcode calendar

我使用下面的代码从组件中获取日期。我有一个 2017-01-08 18:38:00 UTC。我使用下面的代码从中获取日期.

 var components = calendar.dateComponents([.year, .month, .day,.weekday
      ], from: eventModel.start_date!)
    print("component day is \(String(describing: components.day!))")

但是当我打印日期时,它给我的日期是 9,而不是 8,那么代码有什么问题。请指导我如何改进这个东西。

最佳答案

打印(日历.时区)。我认为这是您本地的时区。

---------更新------------

var cps = DateComponents()
cps.year = 2017
cps.month = 1
cps.day = 8
cps.hour = 18
cps.minute = 30

print(cps)

var cal = Calendar(identifier: .gregorian)
let localDate = cal.date(from: cps)!
print(localDate) // 2017-01-08 10:30:00 +0000

let timezone = TimeZone(abbreviation: "UTC")
cal.timeZone = timezone!
let utcDate = cal.date(from: cps)!
print(utcDate) // 2017-01-08 18:30:00 +0000

关于ios - 在 swift 中使用组件日期时获取递增的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41828474/

相关文章:

cocoa - 在 Grand Central Dispatch 中使用 dispatch_sync

swift - 分块请求中未调用 URLSession didReceiveData

ios - 为什么 xcode 存档成功但创建一个空的 .xcarchive 文件?

swift - Swift 中的通用/多态搜索

ios - IOS 5 应用程序中的多个 UIPickerView

xcode - NSTimer 无法在设备后台运行

ios - AutoLayout:我可以指定在需要时应该打破哪个约束并设置备份吗?

objective-c - 如何通过Email Id获取社交网络资料信息?

objective-c - 标签包装太长

ios - 如何在 swift 3 中按下按钮时转到另一个 View Controller