본문 바로가기
iOS/iOS

Date

by 패쓰킴 2022. 6. 10.
728x90

calendar와 date 시간차이

date는 0시 기준

calendar는 +9시간 기준

따라서, 9시간의 차이를 가지고 원하는 날짜를 뽑아오면 됨

 

Calendar

weekdaySymbols

(lldb) po calendar.weekdaySymbols
▿ 7 elements
  - 0 : "Sunday"
  - 1 : "Monday"
  - 2 : "Tuesday"
  - 3 : "Wednesday"
  - 4 : "Thursday"
  - 5 : "Friday"
  - 6 : "Saturday"

(lldb) po calendar.shortWeekdaySymbols
▿ 7 elements
  - 0 : "Sun"
  - 1 : "Mon"
  - 2 : "Tue"
  - 3 : "Wed"
  - 4 : "Thu"
  - 5 : "Fri"
  - 6 : "Sat"

(lldb) po calendar.standaloneWeekdaySymbols
▿ 7 elements
  - 0 : "Sunday"
  - 1 : "Monday"
  - 2 : "Tuesday"
  - 3 : "Wednesday"
  - 4 : "Thursday"
  - 5 : "Friday"
  - 6 : "Saturday"

(lldb) po calendar.veryShortWeekdaySymbols
▿ 7 elements
  - 0 : "S"
  - 1 : "M"
  - 2 : "T"
  - 3 : "W"
  - 4 : "T"
  - 5 : "F"
  - 6 : "S"

(lldb) po calendar.shortStandaloneWeekdaySymbols
▿ 7 elements
  - 0 : "Sun"
  - 1 : "Mon"
  - 2 : "Tue"
  - 3 : "Wed"
  - 4 : "Thu"
  - 5 : "Fri"
  - 6 : "Sat"

(lldb) po calendar.veryShortStandaloneWeekdaySymbols
▿ 7 elements
  - 0 : "S"
  - 1 : "M"
  - 2 : "T"
  - 3 : "W"
  - 4 : "T"
  - 5 : "F"
  - 6 : "S"

DateComponents

weekday = from day의 요일이 어떤 요일인지 리턴해줌

예> from이 2020년 12월 31일일 경우 weekday는 5 즉, 목요일임을 알 수 있다.

 

weekOfMonth = from의 날짜가 몇 주 차인지 리턴해줌

예> from이 2022년 1월 10일 경우 리턴은 3

                   2022년 1월 17일 경우 리턴은 4

 

특정 date 만들기

let calendar = Calendar.current
let now = Date()
let today = calendar.startOfDay(for: now) 
var components = DateComponents()
components.month = 1
calendar.date(byAdding: components, to: now) // 2022-02-10

날짜 차이

components.year = 2023
components.month = 1
components.day = 20

let specialDay = calendar.date(from: components)

components = calendar.dateComponents([.month], from: specialDay!, to: now)
components.month // -12
728x90

댓글