【问题标题】:How to get the today's and tomorrow's date in swift 4如何在swift 4中获取今天和明天的日期
【发布时间】:2019-01-08 01:10:33
【问题描述】:

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

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

【问题讨论】:

  • 你得到的价值和你想要的价值有什么不同?
  • Date().timeIntervalSince1970 - 应该是从 unix 纪元开始的秒数
  • How to add days to a date in swift 3 应该为您提供第二部分的起点......而且您也不应该在 Java 中使用 Calendar
  • @MadProgrammer 这将从上午 12 点开始添加今天的时间。我只想要今天的日期@ 00:00:00 的日期。 (时:分:秒)

标签: ios swift time swift4


【解决方案1】:

这可以使用以下代码非常简单地完成。无需日期组件或其他复杂情况。

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

【讨论】:

    【解决方案2】:

    我会用组件来做这个。

    假设您需要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())
    

    【讨论】:

    • 感谢您的回答。您能否将timeIntervalSince1970 1000 相乘以获得完整的解决方案
    • 一切尽在我想。 time(2) 以秒为单位,但time(3) 以毫秒为单位。我老了,所以我假设time(2)
    【解决方案3】:

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

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

      【讨论】:

        【解决方案5】:

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

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-18
          • 2015-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-28
          • 2019-10-30
          相关资源
          最近更新 更多