nsdate - Objective-c date difference confusion -


i'm trying number of days between 2 dates , logic seems work fine wide range of dates - august 12, 2012 - august 12, 2013 - returns 364 days, 365 if include end day in calculation. however, if plug in august 8, 2013 - august 12, 2013 0, or 1 if adding end day calculation. not sure doing wrong here:

nsstring* strnow = @"august 12, 2013"; nsstring* strthen = @"august 8, 2012";  nsstring* strnumofdays = [datemanager getdatedifference:strthen :strnow]; 

method

- (nsstring*) getdatedifference : (nsstring*) startdate : (nsstring*) enddate {      //convert strings dates     nsdate* datestart = [self convertstringtodate:startdate];     nsdate* dateend = [self convertstringtodate:enddate];      //check make sure second date later first      //get number of days between     nscalendar* gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar];     nsdatecomponents* components = [gregoriancalendar components:nsdaycalendarunit fromdate:datestart todate:dateend options:0];      //the plus 1 including enddate in count     return [nsstring stringwithformat:@"%i", [components day]+1]; } 

convert string date method

- (nsdate*) convertstringtodate : (nsstring*) strtoconvert {      nserror* error = nil;     nsdatadetector* detector = [nsdatadetector datadetectorwithtypes:(nstextcheckingtypes)nstextcheckingtypedate error:&error];     nsarray* arrdatematches = [detector matchesinstring:strtoconvert options:0 range:nsmakerange(0, [strtoconvert length])];      (nstextcheckingresult* match in arrdatematches) {         strtoconvert =  [self convertdatetostring:match.date];     }      nsdate* mydate = [dateformatter datefromstring:strtoconvert];     return  mydate; } 

@wain - conversion working expected. @rob - using nstextcheckingtypes because not know format date submitted - 08/12/2013, august 8, 2013, 2013 8 12, etc.

you can simplify convertstringtodate. shouldn't bother taking date nstextcheckingresult, converting string, , converting date. should simply:

- (nsdate*) convertstringtodate : (nsstring*) string {     nserror *error = nil;     nsdatadetector *detector = [nsdatadetector datadetectorwithtypes:(nstextcheckingtypes)nstextcheckingtypedate error:&error];     if (error)         nslog(@"%s: datadetectorwithtypes error: %@", __function__, error);      nstextcheckingresult* match = [detector firstmatchinstring:string options:0 range:nsmakerange(0, [string length])];      return match.date; } 

i'm unclear why you're adding 1 date difference. i'd inclined just:

- (nsstring*) getdatedifferencebetweenstart:(nsstring*)startdate end:(nsstring*) enddate {     //convert strings dates     nsdate* datestart = [self convertstringtodate:startdate];     nsdate* dateend = [self convertstringtodate:enddate];      //get number of days between     nscalendar* gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar];     nsdatecomponents* components = [gregoriancalendar components:nsdaycalendarunit fromdate:datestart todate:dateend options:0];      return [nsstring stringwithformat:@"%i", [components day]]; } 

note, i'm using more common syntax of identifying names of parameters:

nsstring *difference = [self getdatedifferencebetweenstart:strthen end:strnow]); 

thus, doing this, results 1 have expected:

  • august 12, 2012 - august 12, 2013 results in 365.
  • august 8, 2013 - august 12, 2013 results in 4.

it's not entirely why routine returned did. may have been convertdatetostring method (which didn't show us), or configuration of date formatter (again, don't know did there, either). above code doesn't require either of , seems work expected.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -