ios - 如何在swift 4中获取今天和明天的日期

标签 ios swift time swift4

如何在 unix-epoch 中获取当前日期?timeIntervalSince1970打印当前时间。有什么办法可以得到今天凌晨 12 点的时间吗?

例如,当前时间是:2018 年 1 月 7 日下午 5:30。 timeIntervalSince1970将打印当前时间,即 1546903800000 .
纪元系统中的当前日期将是 2018 年 1 月 7 日上午 00:00。即 1546848000000

最佳答案

这可以使用以下代码非常简单地完成。不需要日期组件或其他复杂功能。

var calendar = Calendar.current
// Use the following line if you want midnight UTC instead of local time
//calendar.timeZone = TimeZone(secondsFromGMT: 0)
let today = Date()
let midnight = calendar.startOfDay(for: today)
let tomorrow = calendar.date(byAdding: .day, value: 1, to: midnight)!

let midnightEpoch = midnight.timeIntervalSince1970
let tomorrowEpoch = tomorrow.timeIntervalSince1970

关于ios - 如何在swift 4中获取今天和明天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54084023/

相关文章:

ios - Background Voice Over 可访问性专注于 UICollectionView

java - 测量 Android 中两个 MotionEvents 之间耗时

ios - `save(to:for:completionHandler:)` 中的 `UIDocument` 崩溃

ios - 调整动画大小后,UIButton 不尊重 Aspect Fill contentMode

ios - WKWebView 的透明背景

ios - 为什么我从 S3 下载的图像只出现在 1 个人的手机上?

Swift 3 Coredata 出错

python - 在 Python 中计算日期是开始、 future 还是现在

javascript - 使用 Javascript 处理时区的最佳方法是什么

objective-c - 启动画面不显示在 iPad 上,但显示在模拟器上