-1

i have a string like 2013-03-05T07:37:26.853 and i want to get the Output : 2013-03-05 07:37:26. I want the final output in NSDate object. i am using below function to convert desire output. and it returning wrong time.

-  (NSDate *) getDateFromCustomString:(NSString *) dateStr 
{    NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T"];
    NSString *datePart = [dateStrParts objectAtIndex:0];
    NSString *timePart = [dateStrParts objectAtIndex:1];
    NSArray *dateParts = [datePart componentsSeparatedByString:@"-"];
    NSArray *timeParts = [timePart componentsSeparatedByString:@":"];

NSDateComponents *components = [[NSDateComponents alloc] init];

[components setHour:[[timeParts objectAtIndex:0] intValue]];
[components setMinute:[[timeParts objectAtIndex:1] intValue]];
[components setSecond:[[timeParts objectAtIndex:2] intValue]];

[components setYear:[[dateParts objectAtIndex:0] intValue]];
[components setMonth:[[dateParts objectAtIndex:1] intValue]];
[components setDay:[[dateParts objectAtIndex:2] intValue]];


NSCalendar *currentCalendar = [NSCalendar currentCalendar];  
NSDate *date = [currentCalendar dateFromComponents:components];
NSLog(@"Date 1:%@",date); // Returns wrong time (2013-03-05 02:07:26)

/* i had tried the below.
NSDateFormatter * format = [[NSDateFormatter alloc] init];
[format setTimeZone:NSTimeZoneNameStyleStandard];
[format setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

NSLog(@"Date 2:%@",[format stringFromDate:date]); // Returns correct date (2013-03-05 07:37:26)

NSDate *fDate = [format dateFromString:[format stringFromDate:date]];
DLog(@"Date 3:%@",fDate); // Returns wrong time (2013-03-05 02:07:26)
*/

[components release];
return  date;
}

Plz suggest me if any idea.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Hitarth
  • 1,950
  • 3
  • 27
  • 52
  • - (NSDate *) getDateFromCustomString:(NSString *) dateStr { // Output : 2013-03-05 07:37:26. NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T0"]; NSString *datePart1 = [dateStrParts objectAtIndex:0]; NSString *timePart = [dateStrParts objectAtIndex:1]; NSArray *datepart2 = [timePart componentsSeparatedByString:@"."]; NSString *date1 = datePart1; NSString *date2 = [datepart2 objectAtIndex:0]; NSLog(@"%@ %@",date1,date2); } – Anjan Mar 09 '13 at 06:32
  • 1
    NSLog of an `NSDate` object always shows the date in GMT. If you take into account your timezone, does the date/time seem correct? – rmaddy Mar 09 '13 at 06:50

3 Answers3

2

What you're seeing here is a time zone issue. If you want to NSLog a correct looking date, we'll need to give the input string a GMT time zone:

Add this in your code and see what happens:

[components setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT: 0]];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

Try this,

    NSString *departTimeDate = @"2013-03-05T07:37:26.853";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS"];

    NSDate *date = [dateFormatter dateFromString:departTimeDate];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSLog(@"Expected Result___ %@",[dateFormatter stringFromDate:date]); //2013-03-05 07:37:26
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • 1
    Why are you stripping off the fractional seconds? Just add ".SSS" to the first date format. – rmaddy Mar 09 '13 at 06:48
0

Try this : issue is Time zone.

+ (NSDate *) getDate:(NSString *) dateStr
{
    NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T"];
    NSString *datePart = [dateStrParts objectAtIndex:0];
    NSString *timePart = [dateStrParts objectAtIndex:1];

    NSString *t = [[timePart componentsSeparatedByString:@"."] objectAtIndex:0];
    NSString *newDateStr = [NSString stringWithFormat:@"%@ %@",datePart,t];
    //2013-03-05 07:37:26

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [df dateFromString:newDateStr];
    DLog(@"%@",date);

    return  date;
}
Maulik
  • 19,348
  • 14
  • 82
  • 137