Pooja
Pooja

Reputation: 2200

convert string into NSDate

i have a "DateTimeArray" this array contain

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  

all the value are in string,and i want to convert this string into NSDate and only want to access time not date
and then this time values will be stored in array and this newly array will be used for drawing line chart

i hope some one know this issue

thank you very much

Upvotes: 1

Views: 1609

Answers (6)

Alex Terente
Alex Terente

Reputation: 12036

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

Upvotes: 1

KingofBliss
KingofBliss

Reputation: 15115

Code as follows,

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

Upvotes: 1

Rams
Rams

Reputation: 1751

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

Upvotes: 0

marzapower
marzapower

Reputation: 5611

Best and cleaner approach:

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

Then to just access hours, and not days, you should select the required components of each NSDate* instance:

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

Upvotes: 1

visakh7
visakh7

Reputation: 26390

Try this

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

Upvotes: 1

Suresh Varma
Suresh Varma

Reputation: 9740

Heres the complete set of code.. Hope it helps..

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

Happy iCoding...

Upvotes: 0

Related Questions