简体   繁体   中英

Is any way for Retrieving current date and time without using [NSDate date]

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..

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM