Bittoo Bittoo - 5 months ago 20
iOS Question

While converting NSString to NSDate?

I try to convert

NSString
to
NSDate
with this code:

NSString *dateString=[[NSString alloc] init];
dateString = [[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm,yyyy/MM/dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date = dateFromString;


But
msg.Date
is
nil
even if
dateString
shows the correct date string. What am I missing?

Answer

change this format

[dateFormatter setDateFormat:@"HH:mm,yyyy/MM/dd"];

into and try

[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];

Update

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
// use simple 
msg.date =   [dateFormatter dateFromString:@"2016/05/06 12:36"];
  NSLog(@"final date == %@",dateFromString);
//NSDate *dateFromString = [dateFormatter dateFromString:@"2016/05/06 12:36"];

// NSLog(@"final date == %@",dateFromString);