-1

지금부터 특정 요일 및 시간 (예 : 다음 달 20 일 오후 6시)까지의 시차 (초)를 계산하려면 어떻게해야합니까?지금부터 임의의 시간과 요일 사이의 초를 계산하십시오.

많은 노력을했지만 많이 계산하지 않고 여러 가지 사례를 확인하지 않고도 쉬운 해결책을 찾지 못했습니다.

NSDate *now = [NSDate date]; 
NSDateComponents *dc = [[NSDateComponents alloc] init]; 
[dc setMonth:1]; 
MyTargetDateObject = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:now options:0]; 
    [dc release]; 

. 지금부터 1 개월 추가합니다. 다른 예는 다음과 같다. here 어떻게 든 적용되지 않습니다. 이 같은

+1

날짜는 _are입니다. 따라서 두 날짜를 형성하고 빼십시오. – matt

답변

1

뭔가 (당신은 스위프트로 번역해야합니다,하지만 난 당신이 아이디어를 얻을 확신) :

let now = NSDate() 
let greg = NSCalendar(calendarIdentifier: NSGregorianCalendar) 
let u : NSCalendarUnit = .CalendarUnitYear | .CalendarUnitMonth 
var dc = greg!.components(u, fromDate: now) 
dc.month += 1 // next month 
dc.hour = 18 // at 6 PM 
dc.day = 20 // on the 20th 
let then = greg!.dateFromComponents(dc)! 
let diff = then.timeIntervalSinceNow // time interval = seconds 

주 우리가 원하는 장치에 추가하여 날짜 연산을 수행 할 수 있습니다 NSDateComponents. 또한 모든 날짜 < -> 날짜 구성 요소 변환은 달력을 통과해야합니다.