abhi.shah88
abhi.shah88

Reputation: 193

Problem in getting proper time

i am not able to get proper time from a given time. My code is as follows

NSArray * array_TimeSlotWith_StartDate = [ @"01/05/2010 10:15:33" componentsSeparatedByString:@" "];
NSArray * array_TimeSlotWith_EndDate = [ @"01/05/2010 10:45:43"   componentsSeparatedByString:@" "];

NSLog(@" array_TimeSlotWith_StartDate  ===   %@",array_TimeSlotWith_StartDate);
NSLog(@"array_TimeSlotWith_EndDate     ==       %@",array_TimeSlotWith_EndDate);

NSArray * Array_StartTime = [[array_TimeSlotWith_StartDate objectAtIndex:1] componentsSeparatedByString:@":"];
NSLog(@"Array_StartTime     ==       %@",Array_StartTime);

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [df dateFromString:[array_TimeSlotWith_StartDate objectAtIndex:1]];
NSDate *date2 = [df dateFromString:[array_TimeSlotWith_EndDate objectAtIndex:1]];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
NSLog(@"date 1%@",date1);
NSLog(@"date 2%@",date2);

It gives me value of date 1 as 1970-01-01 04:45:33 +0000 instead of this what i need is just the time an that should be 10:15:33.

And same for the end date also it gives log as 1970-01-01 05:15:43 +0000 instead of this it should output as only the time component with value 10:45:43 .

Upvotes: 1

Views: 119

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

Time doesn't exist without a date. If you don't specify any date, the date formatter will default to "1970-01-01"(Unix epoch). I hope this SO link will help you.

Though the date1 and date2 don't contain the value you've expected, I feel that, interval should contain the correct result you need. I suggest you to just ignore the dates in this context.

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

you neither specifying the date and timezone that's the reason NSDate object pick the default one for its use.

Upvotes: 0

Related Questions