iOS - Unable to parse date string "31-07-2014 00:00 BST" to get string 08-31-14 using NSDateFormatter -


need convert date string different format

original: dd-mm-yyyy hh:mm bst expected: mm-dd-yy

tried below code, not working

nsdateformatter *dateformatter = [nsdateformatter new];  dateformatter.dateformat =  @"dd-mm-yyyy hh:mm bst";  nsdate *dateformat = [[nsdate alloc] init];  dateformat = [dateformatter datefromstring:datestring]; [dateformatter setdateformat:@"dd-mm-yy"]; nslog(@"formatted date: %@", [dateformatter stringfromdate:dateformat]); return [dateformatter stringfromdate:dateformat]; 

this should work:

nsstring *datestring = @"31-07-2014 00:00 bst"; nsdateformatter *dateformatter = [nsdateformatter new]; [dateformatter settimezone:[nstimezone timezonewithname:@"bst"]]; [dateformatter setdateformat:@"dd-mm-yyyyhh:mm 'bst'"];  nsdate *dateformat = [[nsdate alloc] init]; dateformat = [dateformatter datefromstring:datestring];  [dateformatter setdateformat:@"dd-mm-yy"]; nslog(@"formatted date: %@", [dateformatter stringfromdate:dateformat]); 

what did here set proper time zone source nsdateformatter , parsed date according it. @ setdateformat. use single quotes indicate, static string part of date format.

it written more dynamically determine correct time zone, string splitting required.

i hope helped.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -