2011-05-23 140 views
1

我有一個「DateTimeArray」這個數組包含將字符串轉換成NSDate的

DateTimeArray[index 0] = 20.05.2011 12:12:50  
DateTimeArray[index 1]= 20.05.2011 12:13:20 
DateTimeArray[index 2]= 20.05.2011 12:20:10 

所有的價值都在串,我想這個字符串轉換成NSDate的轉換,只需要訪問時間不日期
和那麼這個時間值將被存儲在陣列和這個新陣將用於繪製折線圖

我希望有人知道這個問題

非常感謝你

回答

1

代碼如下,

for(int index=0;index<[DateTimeArray count];index++) 
{ 
    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    [df setDateFormat:@"HH:mm:ss"]; 
    NSString *timeString=[[[DateTimeArray objectAtIndex:index]componentsSeparatedByString:@" "]objectAtIndex:1]; 
    NSDate *time=[df dateFromString:timeString]; 
    [timeArray addObject:time]; 
    [df release]; 
} 
+0

@KingoofBliss ,非常感謝你 – Pooja 2011-05-23 10:29:32

+0

@pooja:歡迎你:) – KingofBliss 2011-05-23 10:36:15

+0

@KingofBliss,你能告訴我如何設置GMT + 1,因爲它顯示我在1小時前 – Pooja 2011-05-23 10:39:46

1

試試這個

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"HH:mm:ss"]; 
NSDate *formattedDate = [df dateFromString:[DateTimeArray objectAtIndex:requiredIndex]]; 
[df release]; 
+2

我會使用以下格式:「dd.MM. yyyy HH:mm:ss「 – rckoenes 2011-05-23 09:54:35

+0

如果格式不匹配,你會從字符串中得到零,所以要小心 – KingofBliss 2011-05-23 10:04:51

0

繼承人的一套完整的代碼..希望它可以幫助..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"HH:mm:ss"]; 
int i=0;//start for{} loop 
NSString *currDate = [formatter dateFromString:[DateTimeArray objectAtIndex:i]]; 
[formatter release]; 

快樂iCoding ...

+0

如果格式不匹配字符串,你會從字符串中得到零,所以要小心 – KingofBliss 2011-05-23 10:04:15

1

最好和更清潔的方法:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"dd.MM.yyyy HH:mm:ss"]; 

for(int i=0; i < [DateTimeArray count]; ++i) 
{ 
    NSString *string = [DateTimeArray objectAtIndex:i]; 
    NSDate *date = [df dateFromString:string]; 
    [timeArray addObject:time]; 
} 

[df release]; // ALWAYS release unused objects 

再到剛剛接觸小時,而不是幾天,你應該選擇每個NSDate*實例所需的組件:

// Get the Gregorian calendar 
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

// Get the date 
NSDate* date = [timeArray objectAtIndex:my_index]; 

// Get the hours, minutes, seconds 
NSDateComponents* hour = [cal components:NSHourCalendarUnit fromDate:date]; 
NSDateComponents* minute = [cal components:NSMinuteCalendarUnit fromDate:date]; 
NSDateComponents* second = [cal components:NSSecondCalendarUnit fromDate:date]; 
0
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
[timeFormat setDateFormat:@"HH:mm:ss"] 
NSString *theTime = [timeFormat dateFromString:date]; 
or 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date]; 
NSInteger hour = [components hour]; 
NSInteger minute = [components minute]; 
1
NSMutableArray *nsdateArray = [NSMutableArray arrayWithCapacity:[DateTimeArray count]]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd.MM.yyyy HH:mm:ss"]; 

    for(NSString *currentString in DateTimeArray){ 
     NSDate *date = [dateFormat dateFromString:currentString]; 
     [nsdateArray addObject:date]; 
    } 
    [dateFormat release]; 
    NSLog(@"ArrayWithDate:%@",[nsdateArray description]);