4

I've tried to google around but I still can't find the best answer.

All I want is very simple, I just want to get the current time in milliseconds.

How can I do that in cocos2d?

Cadrick Loh
  • 721
  • 1
  • 7
  • 19
  • 1
    I think this has been discussed here: http://stackoverflow.com/questions/889380/how-can-i-get-a-precise-time-for-example-in-milliseconds-in-objective-c –  Aug 11 '11 at 15:14
  • 1
    Or here: http://stackoverflow.com/questions/358207/iphone-how-to-get-current-milliseconds – Yannick Loriot Aug 14 '11 at 18:46

5 Answers5

4

First, a class variable:

CGFloat gameTime;

Then in your class initialize:

[self scheduleUpdate];

Finally, while still in your class:

- (void) update:(ccTime)delta {
    gameTime += delta;
}

delta is the milliseconds since the last call of update. Save gameTime somewhere in a database for lifetime gameTime.

Kheldar
  • 5,361
  • 3
  • 34
  • 63
2
try this

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

CCLog("year------->%04d",timeinfo->tm_year+1900);
CCLog("month------->%02d",timeinfo->tm_mon+1);
CCLog("day------->%02d",timeinfo->tm_mday);

CCLog("hour------->%02d",timeinfo->tm_hour);
CCLog("mintus------->%02d",timeinfo->tm_min);
CCLog("seconds------->%02d",timeinfo->tm_sec);
KARTHIK RA
  • 469
  • 2
  • 8
1

Why not convert the current time into a float value, then multiply the current time by 10^3 to convert it into milliseconds

rich
  • 2,136
  • 19
  • 23
0

To get only milliseconds

NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"ss"];

NSString* str = [formatter stringFromDate:date];

float seconds = str.intValue;

float miliSeconds = seconds / 1000; // HERE is your answer

If you want to get full time format replace

[formatter setDateFormat:@"hh:mm:ss"];
matthias krull
  • 4,389
  • 3
  • 34
  • 54
0

Take the current time in seconds and multiply by 1000 go get number of milliseconds:

double ms = CFAbsoluteTimeGetCurrent() * 1000.0;    
progrmr
  • 75,956
  • 16
  • 112
  • 147