如何在swift 4 中获取今天和明天的日期
How to get the today's and tomorrow's date in swift 4
如何获取unix-epoch
中的当前日期?
timeIntervalSince1970
打印当前时间。有什么办法可以得到今天中午 12 点的时间吗?
例如,当前时间是:2018 年 1 月 7 日 5:30 下午。 timeIntervalSince1970
将打印当前时间,即 1546903800000
。
纪元系统中的当前日期为 2018 年 1 月 7 日 00:00 AM。即 1546848000000
我会用组件来做。
假设您需要 time(2)
. If you need in milliseconds as defined by 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())
这可以使用以下代码非常简单地完成。不需要日期组件或其他并发症。
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
也尝试在日期扩展中添加以下代码:
extension Date
{
var startOfDay: Date
{
return Calendar.current.startOfDay(for: self)
}
func getDate(dayDifference: Int) -> Date {
var components = DateComponents()
components.day = dayDifference
return Calendar.current.date(byAdding: components, to:startOfDay)!
}
}
您可以使用以下方法通过添加天或月或年来获取任何日期
通过指定日历组件和该组件的增量值:
func getSpecificDate(byAdding component: Calendar.Component, value: Int) -> Date {
let noon = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
return Calendar.current.date(byAdding: component, value: value, to: noon)!
}
组件将是以下选项之一:
( .day , .month , .year ) 并且该值将是您要为此组件添加的金额
例如,要获取下一年的日期,您可以使用以下代码:
var nextYear = getSpecificDate(byAdding: .year, value: 1).timeIntervalSince1970
使用此扩展程序获取今天和明天的日期
extension Date {
static var tomorrow: Date { return Date().dayAfter }
static var today: Date {return Date()}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: Date())!
}
}
如何获取unix-epoch
中的当前日期?
timeIntervalSince1970
打印当前时间。有什么办法可以得到今天中午 12 点的时间吗?
例如,当前时间是:2018 年 1 月 7 日 5:30 下午。 timeIntervalSince1970
将打印当前时间,即 1546903800000
。
纪元系统中的当前日期为 2018 年 1 月 7 日 00:00 AM。即 1546848000000
我会用组件来做。
假设您需要 time(2)
. If you need in milliseconds as defined by 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())
这可以使用以下代码非常简单地完成。不需要日期组件或其他并发症。
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
也尝试在日期扩展中添加以下代码:
extension Date
{
var startOfDay: Date
{
return Calendar.current.startOfDay(for: self)
}
func getDate(dayDifference: Int) -> Date {
var components = DateComponents()
components.day = dayDifference
return Calendar.current.date(byAdding: components, to:startOfDay)!
}
}
您可以使用以下方法通过添加天或月或年来获取任何日期 通过指定日历组件和该组件的增量值:
func getSpecificDate(byAdding component: Calendar.Component, value: Int) -> Date {
let noon = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
return Calendar.current.date(byAdding: component, value: value, to: noon)!
}
组件将是以下选项之一: ( .day , .month , .year ) 并且该值将是您要为此组件添加的金额
例如,要获取下一年的日期,您可以使用以下代码:
var nextYear = getSpecificDate(byAdding: .year, value: 1).timeIntervalSince1970
使用此扩展程序获取今天和明天的日期
extension Date {
static var tomorrow: Date { return Date().dayAfter }
static var today: Date {return Date()}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: Date())!
}
}