2016-11-21 3 views
1

나는 둘러 보았고 필요한 것을 찾지 못했습니다.평일과 한시간으로 신속하게 날짜 개체 만들기

여기에 내가 필요로하는 작업은 다음과 같습니다

스위프트, 나는 그 주말 동안 날짜 (또는있는 NSDate) 요일을 나타내는 객체, 그리고 특정 시간을 만들려고합니다. 나는 몇 년이나 몇 달을 걱정하지 않는다.

주말마다 반복되는 이벤트 (특정 평일에는 "매주 월요일 오후 8시"와 같은 특정 시간에 모임) 시스템이 있기 때문입니다.

는 여기에 지금까지 (작동하지 않는)이 코드의이에 날짜를 구문 분석하는 방법

/* ################################################################## */ 
/** 
:returns: a Date object, with the weekday and time of the meeting. 
*/ 
var startTimeAndDay: Date! { 
    get { 
     var ret: Date! = nil 
     if let time = self["start_time"] { 
      let timeComponents = time.components(separatedBy: ":") 
      let myCalendar:Calendar = Calendar.init(identifier: Calendar.Identifier.gregorian) 
      // Create our answer from the components of the result. 
      let myComponents: DateComponents = DateComponents(calendar: myCalendar, timeZone: nil, era: nil, year: nil, month: nil, day: nil, hour: Int(timeComponents[0])!, minute: Int(timeComponents[1])!, second: nil, nanosecond: nil, weekday: self.weekdayIndex, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil) 
      ret = myCalendar.date(from: myComponents) 
     } 

     return ret 
    } 
} 

많은,하지만 나중에 구문 분석하는 Date 객체를 만들려고합니다.

도움을 주시면 감사하겠습니다.

+2

(NS) 날짜 시간 절대 지점이며 주중 시간, 일정 시간대 등 EKRecurrenceRule]에 대해 아무것도 알고 (https://developer.apple. com/reference/eventkit/ekrecurrencerule)을 사용하는 것이 좋습니다 (또는 간단하게 유지하려는 경우 DateComponents). –

+1

관련이 없지만'DateComponents'의 모든 구성 요소에는 기본'nil' 값이 있습니다. 즉, 사용되지 않는 구성 요소를 모두 생략 할 수 있습니다. – vadian

+0

그래, DateComponents가 가장 좋은 방법 일 것 같아. 답으로 문구를 쓰고 싶으면 녹색 확인을 해 드리겠습니다. –

답변

1

(NS)Date 시간에 절대 지점을 나타냅니다 내부적으로는 "기준일"2001년 1월 1일, GMT 이후의 초 수와 표현 평일, 시간, 일정, 시간대 등에 대해 아무것도 모른다.

EventKit으로 작업하는 경우 EKRecurrenceRule은 이 더 적합 할 수 있습니다. 반복 이벤트에 대한 반복 패턴을 설명하는 데 사용되는 클래스입니다.

또는 이벤트를 DateComponentsValue과 같이 저장하고 콘크리트를 Date으로 계산합니다.

예 : 회의 오후 8시 매주 월요일 :

let meetingEvent = DateComponents(hour: 20, weekday: 2) 

다음 회의는? 출력

let now = Date() 
let cal = Calendar.current 
if let nextMeeting = cal.nextDate(after: now, matching: meetingEvent, matchingPolicy: .strict) { 
    print("now:", DateFormatter.localizedString(from: now, dateStyle: .short, timeStyle: .short)) 
    print("next meeting:", DateFormatter.localizedString(from: nextMeeting, dateStyle: .short, timeStyle: .short)) 
} 

:

 
now: 21.11.16, 20:20 
next meeting: 28.11.16, 20:00 
+0

감사! 다음 날짜 예를 들어 주셔서 감사합니다. –