0

I make a application in which i need to show a 30 min countdown timer,after 30 min i need to show that coupons is expired.It work fine.

{    
NSInteger seconds = [[NSDate date] timeIntervalSinceDate:dateDue];
}

But if somebody change the mobile date or time, the timer is disturb and it show wrong values.So is this any way to find current date and time without using [NSDate date] so i use it for my countdown timer.Please give your suggestion..

VSP
  • 166
  • 11

2 Answers2

0

What you are looking for is CACurrentMediaTime(). Or mach_absolute_time().

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • CACurrentMediaTime() is not a timer. It's a wrapper around the most accurate time function in the system: mach_absolute_time(). So if you really care what time it is right now, and you'd like that in a human-understandable number, CACurrentMediaTime() is your function. mach_absolute_time() will give you a really accurate number, but it's based on the Mach absolute time unit which doesn't actually map to anything pesky humans think in (and every CPU has a different scale). That's why we have CACurrentMediaTime() to make our lives easier. – Dimple Shah Nov 10 '15 at 05:43
0

Objective c is just c at its heart; you can use any standard ansi-c compliant methods such as:

time_t time;
struct tm * timeinfo; 
time (&time);
timeinfo = localtime (&time);

Now that you have a local representation of the time, you can get at particulars such as:

Year:  "timeinfo->tm_year+1900;"
Month: "timeinfo->tm_mon+1;"
Date:  "timeinfo->tm_mday;"
Hour:  "timeinfo->tm_hour;"
Mins:  "timeinfo->tm_min;"
Secs:  "timeinfo->tm_sec;"

This time is local; to get GMT time, replace the call to localtime() with gmtime().

Sourced gratefully from: https://stackoverflow.com/a/31646117/2748303

Happy Hacking!

Edit: It seems like what you really want is an absolute epoc timestamp for when the counter starts, and one for when it finishes.

In that case, I would use:

time_t now = time(0);

to return the milliseconds since epoc, and do your comparisons appropriately.

Community
  • 1
  • 1
ObiDan
  • 131
  • 6
  • Thanks for your time but my problem is that i need a datetime which is not affected even the mobile user change the time or date from setting, so i can run my count down based on that datetime.I use to dates for countdown timer one is when it is started and other one is current and i take difference time between two dates and show that timer – VSP Nov 10 '15 at 06:02