如何快速获取今天和明天的日期4

阿米特·帕尔(Amit Pal)

如何获取当前日期unix-epochtimeIntervalSince1970打印当前时间。有什么办法可以获取今天凌晨12点的时间?

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

杰弗里·托马斯(Jeffery Thomas)

我会用组件来做到这一点。

假设您需要按秒定义的时间time(2)如果您需要以定义的毫秒数time(3),则可以将其乘以1000。

// Get right now as it's `DateComponents`.
let now = Calendar.current.dateComponents(in: .current, from: Date())

// Create the start of the day in `DateComponents` by leaving off the time.
let today = DateComponents(year: now.year, month: now.month, day: now.day)
let dateToday = Calendar.current.date(from: today)!
print(dateToday.timeIntervalSince1970)

// Add 1 to the day to get tomorrow.
// Don't worry about month and year wraps, the API handles that.
let tomorrow = DateComponents(year: now.year, month: now.month, day: now.day! + 1)
let dateTomorrow = Calendar.current.date(from: tomorrow)!
print(dateTomorrow.timeIntervalSince1970)

您可以减去1得到昨天。


如果您需要在世界通用时间(UTC,GMT,Z…您给世界通用名称指定的任何名称),请使用以下命令。

let utc = TimeZone(abbreviation: "UTC")!
let now = Calendar.current.dateComponents(in: utc, from: Date())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在我的日期前添加今天和明天的日期

如何停止dialogflow始终返回应该解析为今天和明天的日期

如何以今天,明天,昨天的形式获取生日日期?

如何使用MySQL创建今天,明天和昨天

如何使用DateFormatter显示昨天,今天和明天

输入今天的日期获取明天的输出

PHP获取今天和昨天的日期

如何显示今天和昨天的使用日期?

今天和明天的PHP Display Hour范围

在今天和明天之间的时间间隔?

Android如何获取明天的日期

日期类在今天和明天返回相同的值-奇怪而有趣的错误

php今天明天和第二天获取,但忽略周末

PHP:如何检查日期是今天,昨天还是明天

如何用“今天”或“明天”将“ sed”替换为日期

获取和格式化今天和昨天的日期

Moment.js:获取与今天相关的日期(即“明天,今天,昨天等”)

如何使用Ecto获取明天的日期时间?

如何找到今天和未来日期之间的天数?

Discord如何格式化今天和昨天的日期?

我如何获得今天和 2 周之间的日期结果

如何选择不按一天的特定时间分组的今天和明天数据计数?

Laravel 6 - 如何让用户不能使用明天和以后的日期

迅速3如何获得明天和昨天的日期(请注意特殊情况)新月或新年

从网页获取明天的日期

在Java中比较日期。今天,明天搜索

如何获取特定日期的明天日期

在Swift中获取今天和给定日期之间的日期数组?

PHP,从日期获取明天的日期