2012-12-25 5 views
1

기본적으로 특정 날짜 (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 !!

+0

당신은, 인쇄, 사용을 로그인 할 때마다 nsdate 객체가 타임 스탬프가 올 때, 당신은 그것을 실행할 수 있습니다. –

+0

[nsdate date]는 현재 날짜를 문자열, dateComponents 및 +1/-1로 변환하고 문자열을 업데이트합니다 ... thats that. –

+0

나는 현재 날짜의 이전 날짜와 다음 날짜를 원하지 않는다. 나는 다음 날짜와 역동적 인 날짜의 이전 날짜를 원한다 !! – Maverik

답변

8

않는 : componets.day = 1 전날에 -1, 다음을 얻을 수있다.

NSDate *date = [NSDate date]; // your date from the server will go here. 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [[NSDateComponents alloc] init]; 
components.day = 1; 
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0]; 

NSLog(@"newDate -> %@",newDate); 
+0

또한, @OP : 당신이'+ alloc'-ed의 모든 것을 릴리즈하는 것을 잊지 마십시오. –

+0

좋아요, 내가 바로 가기이므로 새로운 것을 선호합니다. alloc + init에 많은 differnce가 있습니까? –

+0

@AnooVaidya 아무런 차이가 없습니다. :) 더 자주 사용되기 때문에'alloc-init'을 선호합니다. 어쨌든 명시 적으로 표시되는 인스턴스를 만듭니다. –

4

아래의 코드는 당신에게 이전 날짜 얻어야한다 :

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
[offsetComponents setDay:-1]; // replace "-1" with "1" to get the datePlusOneDay 
NSDate *dateMinusOneDay = [gregorian dateByAddingComponents:offsetComponents toDate:myDate options:0]; 

메리 크리스마스 :)

1
NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[[NSDate alloc] init]]; 

[components setHour:-[components hour]]; 
[components setMinute:-[components minute]]; 
[components setSecond:-[components second]]; 
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight); 

[components setHour:-24]; 
[components setMinute:0]; 
[components setSecond:0]; 
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0]; 
1

하는 것은 간단한 이전 날짜에 대한 솔루션을보십시오 : -

-(void)previousDay 
{ 
    NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd/MM/yyyy"]; 
    dateString = [dateFormat stringFromDate:today]; 


    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *comps = [NSDateComponents new]; 
    comps.day =-1; 
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]; 
    [dateFormat setDateFormat:@"dd/MM/yyyy"]; 
    EndingDate = [dateFormat stringFromDate:sevenDays]; 

} 

기능 다음날 previousDay

에 대한

NSDate *datePlusOneDay = [[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 24)]; 
NSLog(@"datePlusOneDay=%@",datePlusOneDay); 
0

기능을

-(void)nextDay 
{ 
    NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd/MM/yyyy"]; 
    dateString = [dateFormat stringFromDate:today]; 


    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *comps = [NSDateComponents new]; 
    comps.day =1; 
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]; 
    [dateFormat setDateFormat:@"dd/MM/yyyy"]; 
    EndingDate = [dateFormat stringFromDate:sevenDays]; 

}