Note that there are some explanatory texts on larger screens.

plurals
  1. PONSString to NSDate return null value
    primarykey
    data
    text
    <p>Here i am getting current time and check whether its in am/pm format. If its not like that, i convert 24 hour time format into 12 hour time format and add AM/PM manually.</p> <p>This is my code:</p> <pre><code>- (NSString *) getCurrentTime { NSDate *today = [NSDate date]; NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init]; [timeFormatter setDateFormat:@"hh:mm:ss a"]; NSString *currentTime = [timeFormatter stringFromDate:today]; currentTime = [self checkTimeFormat:currentTime]; return currentTime; } </code></pre> <p>And</p> <pre><code> - (NSString *) checkTimeFormat:(NSString *) currentTime { NSArray *timeArray = [currentTime componentsSeparatedByString:@":"]; int intHour = [[timeArray objectAtIndex:0] intValue]; NSString *lastVal = [timeArray objectAtIndex:2]; if ([lastVal rangeOfString:@"M"].location == NSNotFound) { if (intHour &lt; 12) lastVal = [NSString stringWithFormat:@"%@ AM", [timeArray objectAtIndex:2]]; else lastVal = [NSString stringWithFormat:@"%@ PM", [timeArray objectAtIndex:2]]; } if (intHour &gt; 12) intHour = intHour - 12; currentTime = [NSString stringWithFormat:@"%d:%@:%@", intHour, [timeArray objectAtIndex:1], lastVal]; NSLog(@"Current Time ==&gt;&gt; %@", currentTime); return currentTime; } </code></pre> <p>Conversion of NSString into NSDate code below:</p> <pre><code>NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm:ss a"]; NSDate *testDate = [dateFormatter dateFromString:getCurrentTime]; NSLog(@"testDate =&gt; %@", testDate); </code></pre> <p>If the Time is in 12 hour format (am/pm), testDate value is getting correctly.</p> <p>If the Time is in 24 hour format, testDate value is <strong>null</strong></p> <p>What is the issue?</p> <p>Thanks in Advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload