【问题标题】:Create own NSDate创建自己的 NSDate
【发布时间】:2015-05-20 16:38:27
【问题描述】:

我想创建一个 NSDate。应该是第1年,第1个月,第1天,第0分钟,第0秒,第0纳秒。我不知道我自己如何创建这一天。那么你们知道如何在 Swift 中做到这一点吗?非常感谢您的帮助!

【问题讨论】:

    标签: swift xcode6 nsdate nscalendar


    【解决方案1】:

    这可以通过使用 NSDateComponents 来完成

    var dateComponents = NSDateComponents()
    dateComponents.second  = 0
    dateComponents.minute = 0
    dateComponents.hour = 0
    dateComponents.day = 1
    dateComponents.month = 1
    dateComponents.year = 1
    dateComponents.nanosecond = 0
    let date = NSCalendar.currentCalendar().dateFromComponents(dateComponents)
    

    【讨论】:

      【解决方案2】:

      使用专门为此目的设计的NSCalendar 方法:

      let myDate = NSCalendar.currentCalendar().dateWithEra(1, year: 1, month: 1, day: 1, hour: 1, minute: 1, second: 1, nanosecond: 0)
      

      【讨论】:

      • 我认为 Patrick 足够聪明,可以查看参数并填写所需的值,而不是盲目地从 StackOverflow 复制和粘贴代码,甚至不阅读它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多