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

问题描述 投票:0回答:6

如何获取

unix-epoch
中的当前日期?
timeIntervalSince1970
打印当前时间。有什么办法可以获取今天中午 12 点的时间吗?

例如,当前时间为:2018 年 1 月 7 日下午 5:30。

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

ios swift time swift4
6个回答
26
投票

使用以下代码可以非常简单地完成此操作。不需要日期组件或其他复杂情况。

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

18
投票

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

假设您需要

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())

12
投票

使用此扩展获取今天和明天的日期

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())!
   }
}

4
投票

还可以尝试在日期扩展中添加以下代码:

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)!
    }
}

0
投票

您可以使用以下方法通过添加天、月或年来获取任何日期 通过指定日历组件和该组件的增量值:

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

0
投票

我使用以下日期扩展来计算今天、明天、昨天、后天、前天、午夜和中午:

extension Date {
    static var     today: Date { Date().midnight      }
    static var yesterday: Date { Date.today.dayBefore }
    static var  tomorrow: Date { Date.today.dayAfter  }
    
    var dayBefore: Date { cal.date(byAdding:.day,value:-1,to:self)! }
    var  dayAfter: Date { cal.date(byAdding:.day,value: 1,to:self)! }
    var  midnight: Date { at(00,00)! }
    var      noon: Date { at(12,00)! }

    func at(_ h:Int,_ m:Int,_ s:Int = 0) -> Date? { cal.date(bySettingHour:h,minute:m,second:s,of:self) }
}

fileprivate var cal: Calendar { Calendar.autoupdatingCurrent }
© www.soinside.com 2019 - 2024. All rights reserved.