Formatting NSDate Problem in getting null when converting NSString to NSDate

hello everyone I am struggling to convert NSString to NSDate using Formatter, but it will be null Can anyone help here is my code ...

 NSString *dateString=[NSString stringWithFormat:@"10/26/2010/09:56:56 PM"];
 NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormat setDateFormat:@"mm/dd/yyyy HH:mm:ss a"];
 NSDate *localDateTime =[dateFormat dateFromString:dateString];
 NSLog(@"client date is %@",localDateTime);

thanks in advance

+3
source share
2 answers

Maybe because your dateString and format do not match.

10/26/2010/09:56:56 PM

mm/dd/yyyy HH:mm:ss a

Do you see the difference? :) They actually have to match. In the format, you have a space between the year and the clock, and there is a slash in the actual date. The month that you compare to is 'mm', which is actually minutes, and should be "MM". Change to:

10/26/2010 09:56:56 PM

MM/dd/yyyy HH:mm:ss a

, , .

+3
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSDate *formatterDate = [dateFormatter dateFromString:@"1999-07-11 at 10:30"];
[dateFormatter release];

.

0

Source: https://habr.com/ru/post/1771432/


All Articles