iphone - Math issue using timeIntervalSince 1970 -


i trying find how many milliseconds current day are. can't find method return time in milliseconds ignoring date, figured calculate off of value returned timeintervalsince 1970 method.

i did this:

nslog(@"%f", [[nsdate date] timeintervalsince1970]); 2013-05-21 16:29:09.453 testapp[13951:c07] 1369171749.453490 

now assumption that, since there 86,400 seconds in day divide value 86400 , how many days have elapsed since 1970. doing gives me 15846.8952483 days. now, if assumption holds, 89.52483% through current day. multiple 24 hours 86.52659% give me current time of 21.4859592 hour or 09:29 pm. can see nslog 5 hours real time, believe interval returned gmt 5 hours ahead of time zone.

so figured, heck, i'll roll , see happens.

i cut off decimal places doing:

float timesince1970 = [[nsdate date] timeintervalsince1970]/86400.0; timesince1970 = timesince1970 - (int)timesince1970 

then calculate milliseconds have taken place far today:

int timenow = timesince1970 * 86400000; nslog(@"%i", timenow); 2013-05-21 16:33:37.793 testapp[14009:c07] 77625000 

then convert milliseconds (which still seem appropriate) nsdate:

nsstring *timestring = [nsstring stringwithformat:@"%d", timenow]; nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; [dateformatter setdateformat:@"a"] nsdate *datenow = [dateformatter datefromstring:timestring]; nslog(@"%@", datenow); 2013-05-21 16:29:09.455 testapp[13951:c07] 2000-01-02 03:29:00 +0000 

and there problem. rather returning 2000-01-01 date hours , minutes attached, returning 2000-01-02 date. why!?

edit

i got working "removing" 5 hours noted in above with:

int timenow = (timesince1970 * 86400000) - (5 * 60 * 60 * 1000); 

i don't understand why necessary though. if can explain i'd appreciate it.

edit 2

perhaps should asking more elementary question how accomplish task i'm trying accomplish. care times (for example, 4pm important care less date). i've been storing these in nsdates created by:

[dateformatter setdateformat:@"hh:mm a"]; [dateformatter datefromstring@"04:00 pm"]; 

all seems going fine. want compare current time saved time , find out if nsorderedascending or nsordereddescending , respond accordingly. there better way accomplishing this?

the part of question says want calculate "how many milliseconds current day are" , "4pm important care less date" makes not answerable.

this because "today" there have been time change, changes number of milliseconds since midnight (by adding or subtracting hour, instance, or leap second @ end of year, etc....) , if don't have date, can't determine number of milliseconds accurately.

now, address edited question: if assume today's date, need use time have stored , combine today's date "specific point in time" can compare current date , time:

nsstring *storedtime = @"04:00 pm";  // use current calendar  nscalendar *cal = [nscalendar currentcalendar];  // create date stored time nsdateformatter *dateformatter = [nsdateformatter new]; [dateformatter setdateformat:@"hh:mm a"]; nsdate *storeddate = [dateformatter datefromstring:storedtime];  // break components (ie hours , minutes) nsdatecomponents *storeddatecomps = [cal components:nshourcalendarunit | nsminutecalendarunit                                             fromdate:storeddate];  // current date/time: nsdate *currentdateandtime = [nsdate date];  // break components (the date portions) nsdatecomponents *todaycomps = [cal components:nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit                                       fromdate:currentdateandtime];  // combine stored time todaycomps.hour = storeddatecomps.hour; todaycomps.minute = storeddatecomps.minute;  // create date comps. // give today's date, time stored nsdate *currentdatewithstoredtime = [cal datefromcomponents:todaycomps];  // now, have current date , stored value date, matter of comparing them: nscomparisonresult result = [currentdateandtime compare:currentdatewithstoredtime]; 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -