0

Possible Duplicate:
NSString to NSDate

Hi I want to convert the string "OCT 1, 2011 18:30" into NSDate format 2011-05-13 19:00:00 +0800. I have tried with the many formatters but what I am getting is a null value each time. Can any body help me on this?

Community
  • 1
  • 1
Shakti
  • 1,889
  • 4
  • 18
  • 29

2 Answers2

1

try this

NSDateFormatter *datefor = [[[NSDateFormatter alloc] init] autorelease];
     [datefor setDateFormat:@"MMM dd, yyyy HH:mm"];
     NSDate *date = [datefor dateFromString:@"OCT 1, 2011 18:30"];
    [datefor setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 
     NSString *str=  [[NSString alloc] initWithString:[datefor stringFromDate:date]];
     NSDate *date_fianl = [datefor dateFromString:str];
      [str release];
     NSLog(@"%@",date_fianl);//it will print 2011-10-01 01:00:00 +0000
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
1

You can try this code that will help you because I run successfully.

    NSString *dateStr = @"OCT 1, 2011 18:30";
    NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
    [dtF setDateFormat:@"LLL d, yyyy HH:mm"];
    NSDate *d = [dtF dateFromString:dateStr];
    NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
    [dateFormatStr setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
    NSDate *st = (NSDate *)[dateFormatStr stringFromDate:d];   
    NSLog(@"%@",st);
Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54