Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DateComponentsと仲良くなる

sato-shin
September 19, 2021

 DateComponentsと仲良くなる

iOSDC 2021で発表したスライドです。
* asken tech blog: https://tech.asken.inc
* youtube link: アップロードされたら

sato-shin

September 19, 2021
Tweet

More Decks by sato-shin

Other Decks in Programming

Transcript

  1. ଍͠ࢉ͢Δ let after10Sec = Date() + 10 let after1Minute =

    Date() + 60 let after1Day = Date() + (60 * 60 * 24) let after1Month = Date() + (60 * 60 * 24 * 30)
  2. ଍͠ࢉ͢Δ ! let after10Sec = Date() + 10 let after1Minute

    = Date() + 60 let after1Day = Date() + (60 * 60 * 24) let after1Month = Date() + (60 * 60 * 24 * 30)
  3. ੜ೥݄೔Λ%BUFͰදݱͯ͠ΈΔ let birthday = Date(timeIntervalSince1970: 656_175_600) let formatter = DateFormatter()

    formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = TimeZone(abbreviation: "UTC") print(formatter.string(from: birthday))
  4. ੜ೥݄೔Λ%BUFͰදݱͯ͠ΈΔ let birthday = Date(timeIntervalSince1970: 656_175_600) let formatter = DateFormatter()

    formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = TimeZone(abbreviation: "UTC") print(formatter.string(from: birthday)) 1990-10-18 00:00:00 +09:00
  5. ੜ೥݄೔Λ%BUFͰදݱͯ͠ΈΔ let birthday = Date(timeIntervalSince1970: 656_175_600) let formatter = DateFormatter()

    formatter.dateFormat = "yyyy-MM-dd" formatter.timeZone = TimeZone(abbreviation: "UTC") print(formatter.string(from: birthday)) 1990-10-18 00:00:00 +09:00 1990-10-17
  6. %BUF$PNQPOFOUT JOJUJBMJ[FS DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?,

    month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  7. %BUF$PNQPOFOUT JOJUJBMJ[FS DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?,

    month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  8. %BUF$PNQPOFOUT JOJUJBMJ[FS ͳΜͱҾ਺ݸʂ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  9. %BUF$PNQPOFOUT JOJUJBMJ[FS શͯ೔࣌ͷߏ੒ཁૉ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  10. %BUF$PNQPOFOUT JOJUJBMJ[FS ΧϨϯμʔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  11. %BUF$PNQPOFOUT JOJUJBMJ[FS λΠϜκʔϯ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  12. %BUF$PNQPOFOUT JOJUJBMJ[FS ࣌୅ ݩ߸ HSFHPSJBOΧϨϯμʔͷ৔߹ لݩલ ੢ྐྵ ࿨ྐྵΧϨϯμʔͷ৔߹ େԽ ฏ੒

    ྩ࿨ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year: Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  13. %BUF$PNQPOFOUT JOJUJBMJ[FS ೥݄೔࣌෼ඵφϊඵ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  14. %BUF$PNQPOFOUT JOJUJBMJ[FS ༵೔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  15. %BUF$PNQPOFOUT JOJUJBMJ[FS ͦͷ༵೔͕Կ൪໨͔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?) ݄ͷୈ ೔༵೔
  16. %BUF$PNQPOFOUT JOJUJBMJ[FS ࢛൒ظ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  17. %BUF$PNQPOFOUT JOJUJBMJ[FS ͦͷ݄ͷԿि໨͔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?) ݄ͷୈि໨
  18. %BUF$PNQPOFOUT JOJUJBMJ[FS ͦͷ೥ͷԿि໨͔ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  19. %BUF$PNQPOFOUT JOJUJBMJ[FS ೥ʢXFFLPGZFBS༻ʣ DateComponents(calendar: Calendar?, timeZone: TimeZone?, era: Int?, year:

    Int?, month: Int?, day: Int?, hour: Int?, minute: Int?, second: Int?, nanosecond: Int?, weekday: Int?, weekdayOrdinal: Int?, quarter: Int?, weekOfMonth: Int?, weekOfYear: Int?, yearForWeekOfYear: Int?)
  20. ೔࣌ DateComponents => Date let today = DateComponents(calendar: gregorianCalendar, year:

    2021, month: 9, day: 19) var todayInJp = today todayInJp.timeZone = TimeZone(abbreviation: "JST") todayInJp.date // 2021-09-19 00:00:00 +09:00
  21. ೔࣌ DateComponents => Date let today = DateComponents(calendar: gregorianCalendar, year:

    2021, month: 9, day: 19) var todayInJp = today todayInJp.timeZone = TimeZone(abbreviation: "JST") todayInJp.date // 2021-09-19 00:00:00 +09:00
  22. ೔࣌ let today = gregorianCalendar .dateComponents([.calendar, .year, .month, .day], from:

    Date()) Date͔Β Calendarͱ೥݄೔͕ઃఆ͞Εͨ DateComponentsΛੜ੒ Date => DateComponents
  23. ೔࣌ ʮࠓ೔ʯͷදݱྫʢ❌ʣ // 2021೥ͷ262೔໨ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, day: 262) today.isValidDate // false 2021೥1݄262೔ͱ൑ఆ͞Εɺfalse
  24. ೔࣌ ʮࠓ೔ʯͷදݱྫʢ❌ʣ // 2021೥ͷୈ38,೔༵೔ let today = DateComponents(calendar: gregorianCalendar, year:

    2021, weekday: 1, weekdayOrdinal: 38) today.isValidDate // false 2021೥1݄ͷୈ38,೔༵೔ͱ ൑ఆ͞Εɺfalse
  25. ྔ ೔࣌ܭࢉͷ͓ڙʹ let oneMonth = DateComponents(month: 1) // ̍ϲ݄ let

    oneMonthLater = calendar.date(byAdding: oneMonth, to: Date())
  26. ྔ ೔࣌ܭࢉͷ͓ڙʹ let oneMonth = DateComponents(month: 1) // ̍ϲ݄ let

    oneMonthLater = calendar.date(byAdding: oneMonth, to: Date())
  27. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30) let

    trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true)
  28. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30) let

    trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true)
  29. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30 weekday:

    1) let trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true)
  30. ύλʔϯϚον UserNotificationͷTriggerදݱͳͲ let wakeUpAlertTime = DateComponents(hour: 8, minute: 30 weekday:

    1) let trigger = UNCalendarNotificationTrigger( dateMatching: wakeUpAlertTime, repeats: true) ೔༵೔ͷ8࣌30෼