gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:28:53 24 4
gpt4 key购买 nike

如何在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com