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?
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);
Use this Code,
NSString *dateString=[[NSString alloc] init];
dateString =[[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date=dateFromString;
hope its helpful
Try this code to convert NSString to NSDate
NSString *dateString = @"2016-05-06";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:@"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:@"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:@"HH:mm:ss zzz"];
[dateFormatter setDateFormat:@"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.