기본적으로 특정 날짜 (x)를 전날 날짜 (x - 1 일)로 변환하고 다음 날 날짜 (x + 1 일)로 변환하고 싶습니다. 다음 날짜와 전날 날짜 받기
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
그러나 나는있는 NSString 형식으로 내 날짜 (X)를 가지고 있고, 나는 위의 코드를 적용하기 전에있는 NSDate (
myDate
)에있는 NSString (
myDateString
)을 변환해야합니다 :이 위해 다음 코드를 사용하고 있습니다. 날짜가 MM-dd-yyyy 인 NSString이 있습니다. 변환을 위해 다음 코드를 사용하고 있지만 불합리한 값을 얻고 있습니다.
NSLog(@"myDateString=%@",myDateString);//output:myDateString=10-25-2012//all correct
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy"];
NSDate *myDate=[formatter dateFromString:myDateString];
NSLog(@"myDate=%@",myDate);//output:myDate=2012-10-24 18:30:00 +0000 // I only want the date to be shown // and why has the format changed
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
NSLog(@"datePlusOneDay=%@",datePlusOneDay);//output:datePlusOneDay=2012-10-25 18:30:00 +0000// I only want the date to come , not time // and why has the format changed
나중에 다시 나는 마찬가지로 나 또한 이전의 날짜를 얻으려면
NSString *curentString=[formatter stringFromDate:datePlusOneDay];
NSLog(@"curentString=%@",curentString); //output:curentString=10-26-2012
있는 NSString
에있는 NSDate 변환해야합니다. 제발 도와주세요 !! 그리고 ho ho MERRY CHRISTMAS !!
당신은, 인쇄, 사용을 로그인 할 때마다 nsdate 객체가 타임 스탬프가 올 때, 당신은 그것을 실행할 수 있습니다. –
[nsdate date]는 현재 날짜를 문자열, dateComponents 및 +1/-1로 변환하고 문자열을 업데이트합니다 ... thats that. –
나는 현재 날짜의 이전 날짜와 다음 날짜를 원하지 않는다. 나는 다음 날짜와 역동적 인 날짜의 이전 날짜를 원한다 !! – Maverik