2012-01-18 64 views
0

我已經從數組中檢索了幾個日期,這些日期以字符串格式存儲在數組中,現在我又想將日期時間更改爲OO:OO:OO當我檢索它並獲取沒有time..My代碼的日期如下將NSDate設置爲特定時間

for(int i = 0 ; i<[keys count]; i++) 
{  
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
       dateFormatter.timeStyle = NSDateFormatterNoStyle; 

       dateFormatter.dateFormat = @"dd-MMM-yyyy";  


      NSDate *noteDate = [dateFormatter dateFromString:[keys objectAtIndex:i]]; 

      NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
      NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit)fromDate:[NSDate date]]; 

      NSInteger year = [weekdayComponents year]; 
      //NSInteger month = [weekdayComponents month]; 


      [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
      NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init]; 

      [timeZoneComps setYear:year]; 
      [timeZoneComps setHour:00]; 
      [timeZoneComps setMinute:00]; 
      [timeZoneComps setSecond:01]; 

      NSDate *noteDate1 =[gregorian dateFromComponents:timeZoneComps];   

      [self saveNotes:0 :noteDate1 :notes]; 
      NSLog(@"dates:%@",noteDate); 
     } 

無論我幹什麼正確的方法或者我失去了一些東西,請朋友們幫我..

問候 蘭吉特

回答

1

將日期格式設置爲

dateFormatter.dateFormat = @"dd-MMM-yyyy HH:mm:ss"; 

也註釋掉該行

dateFormatter.timeStyle = NSDateFormatterNoStyle; 

,然後再試一次