2012-12-25 124 views
1

我基本上想要將特定日期(x)轉換爲前一天日期(x - 1天)以及第二天日期(x + 1天)。我使用下面的代碼是:獲取下一個日期和前一天日期

NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)]; 

但是我有我的NSString格式日期(x)和我需要將上面的代碼之前NSString的(myDateString)轉換成的NSDate(myDate)。 我有一個NSString格式的MM-dd-yyyy格式的日期。 對於轉換我使用下面的代碼,但我越來越荒唐的價值觀。

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 

再後來,我需要到的NSDate轉換爲NSString的

NSString *curentString=[formatter stringFromDate:datePlusOneDay]; 
NSLog(@"curentString=%@",curentString); //output:curentString=10-26-2012 

同樣我也希望得到一個日期。 請幫助傢伙!和浩浩聖誕快樂!

+0

無論何時登錄,打印,使用nsdate對象的時間戳即將到來,您可以從中運行。 –

+0

[nsdate date]會給你當前的日期,把它轉換成字符串或dateComponents和+1/-1並更新字符串......就是這樣。 –

+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分配的內容。 – 2012-12-25 09:11:59

+0

好吧,我更喜歡新的,因爲它是快捷方式,是否有很多不同的alloc + init –

+0

@AnooVaidya沒有區別,真的:)我只是更喜歡'alloc-init',因爲它更經常使用,而且無論如何它使機制創建一個明確可見的實例。 – 2012-12-25 09:13:15

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

嘗試使用以前的日期這樣簡單的解決方案: -

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

功能previousDay

-(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]; 

} 

功能第二天

-(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]; 

} 
相關問題