ios - 24 hour time from date string -
i trying time , in 24 hour format following string :
7/2/2015 2:30:00 pm
this have tried :
-(nsstring*)returndate:(nsstring*)datestring { nsdateformatter *dateformatter1 = [[nsdateformatter alloc] init]; [dateformatter1 setdateformat:@"mm/dd/yyyy hh:mm:ss aaa"]; nsdate *date = [dateformatter1 datefromstring:datestring]; datestring = [date descriptionwithlocale:[nslocale systemlocale]]; nstimezone *currenttimezone = [nstimezone localtimezone]; nstimezone *utctimezone = [nstimezone timezonewithabbreviation:@"utc"]; nsinteger currentgmtoffset = [currenttimezone secondsfromgmtfordate:date]; nsinteger gmtoffset = [utctimezone secondsfromgmtfordate:date]; nstimeinterval gmtinterval = currentgmtoffset - gmtoffset; nsdate *destinationdate = [[nsdate alloc] initwithtimeinterval:gmtinterval sincedate:date]; nsdateformatter *dateformatters = [[nsdateformatter alloc] init]; [dateformatters setdateformat:@"hh:mm a"]; [dateformatters setdatestyle:nsdateformattershortstyle]; [dateformatters settimestyle:nsdateformattershortstyle]; [dateformatters setdoesrelativedateformatting:yes]; [dateformatters settimezone:[nstimezone systemtimezone]]; datestring = [dateformatters stringfromdate: destinationdate]; return datestring; }
output : 4:30
this returning correct time in 12 hour format. want time in 24 hour format.
desired output : 16:30
there way code in question, try this:
nsstring *datestring = @"7/2/2015 4:30:00 pm"; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter settimezone:[nstimezone systemtimezone]]; [dateformatter setdateformat:@"mm/dd/yyyy hh:mm:ss aaa"]; nsdate *date = [dateformatter datefromstring:datestring]; [dateformatter setdateformat:@"hh:mm"]; datestring = [dateformatter stringfromdate: date]; nslog(@"datestring: %@", datestring);
output:
datestring: 16:30
Comments
Post a Comment