Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Date / Time / Calendar / Time zone

Date / Time / Calendar / Time zone

Presentation about classes from iOS SDK: Date / Time / Calendar / Time zone

Avatar for Lukasz Pikor

Lukasz Pikor

May 25, 2017
Tweet

More Decks by Lukasz Pikor

Other Decks in Programming

Transcript

  1. Date / Time / Calendar / Time zone Date /

    Time / Calendar / Time zone
  2. 60, 3600, 86 400 60 * 60 (60 * 60

    * 7, 60 * 60 * 17) [2, 3, 4, 5, 6]
  3. 60, 3600, 86 400 // extension Date var isTomorrow: Bool

    { let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) }
  4. 60, 3600, 86 400 // extension Date var isTomorrow: Bool

    { let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } let dayInterval = TimeInterval(60 * 60 * 24)
  5. 60, 3600, 86 400 // extension Date var isTomorrow: Bool

    { let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } let tomorrow = Date().addingTimeInterval(dayInterval)
  6. 60, 3600, 86 400 // extension Date var isTomorrow: Bool

    { let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } guard let endOfTomorrow = tomorrow.endOfDay else { return false }
  7. 60, 3600, 86 400 // extension Date var isTomorrow: Bool

    { let dayInterval = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow)
  8. Date init(timeIntervalSinceReferenceDate: TimeInterval) // reference date: 00:00:00 UTC on 1

    January 2001 init(timeIntervalSince1970: TimeInterval) // Unix Epoch: 00:00:00 UTC on 1 January 1970
  9. DateComponents let calendar = Calendar.current let timezone = TimeZone(identifier: "Europe/Warsaw")

    var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-25 17:00:00 +0000
  10. DateComponents let calendar = Calendar.current let timezone = TimeZone(identifier: "Europe/Warsaw")

    var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-25 17:00:00 +0000 var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone
  11. DateComponents let calendar = Calendar.current let timezone = TimeZone(identifier: "America/Los_Angeles")

    var dateComps = DateComponents() dateComps.calendar = calendar dateComps.year = 2017 dateComps.month = 5 dateComps.day = 25 dateComps.hour = 19 dateComps.timeZone = timezone let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-26 02:00:00 +0000 let dateToPrint = dateComps.date! print("Date: \(dateToPrint)”) // Date: 2017-05-26 02:00:00 +0000
  12. DateComponents let now = Date() var comps = DateComponents() comps.month

    = -1 comps.day = -1 comps.hour = -1 comps.timeZone = timezone let calendar = Calendar.current let newDate = calendar.date(byAdding: comps, to: now) // Apr 24, 2017, 5:30 PM let now = Date() var comps = DateComponents() comps.month = -1 comps.day = -1 comps.hour = -1 comps.timeZone = timezone
  13. DateComponents let now = Date() var comps = DateComponents() comps.month

    = -1 comps.day = -1 comps.hour = -1 comps.timeZone = timezone let calendar = Calendar.current let newDate = calendar.date(byAdding: comps, to: now) // Apr 24, 2017, 5:30 PM let calendar = Calendar.current let newDate = calendar.date(byAdding: comps, to: now) // Apr 24, 2017, 5:30 PM
  14. DateFormatter let now = Date(timeIntervalSince1970: 1495737000) let formatter = DateFormatter()

    formatter.locale = Locale.autoupdatingCurrent formatter.dateStyle = .medium formatter.timeStyle = .medium let output = formatter.string(from: now) // shorter version: DateFormatter.localizedString(from: now, dateStyle: .medium, timeStyle: .medium) // "May 25, 2017, 8:30:00 PM" // "2017年年5⽉月25⽇日 下午8:30:00"
  15. So… let tuple = (60 * 60 * 7, 60

    * 60 * 17) var sevenAM = DateComponents() sevenAM.hour = 7 sevenAM.minute = 00 var fivePM = DateComponents() sevenAM.hour = 17 sevenAM.minute = 00
  16. So… // extension Date var isTomorrow: Bool { let dayInterval

    = TimeInterval(60 * 60 * 24) let tomorrow = Date().addingTimeInterval(dayInterval) guard let endOfTomorrow = tomorrow.endOfDay else { return false } return isBetween(startDate: tomorrow.startOfDay, endDate: endOfTomorrow) } calendar.isDateInTomorrow(myDate)