cocos2d-x3.3获取时间

前端之家收集整理的这篇文章主要介绍了cocos2d-x3.3获取时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


一、获取时间

struct tm *tm;

time_t timep;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
time(&timep);
#else
struct timeval tv;
gettimeofday(&tv,NULL);
timep = tv.tv_sec;
#endif
tm = localtime(&timep);
int year = tm->tm_year + 1900;//年
int month = tm->tm_mon + 1;//月

int day = tm->tm_mday;//日

二、获取两个日期相差多少天

1、获取当前日期的天数

GetAbsDays(MyDate x) {
int i;
int month_day[] = { 31,28,31,30,31 };
int year = x.year - 1;
int days = year * 365 + year / 4 - year / 100 + year / 400;
if (x.year % 4 == 0 && x.year % 100 != 0 || x.year % 400 == 0)
month_day[1]++;
for (i = 0; i < x.month - 1; i++)
days += month_day[i];
days += x.day - 1;
return days;
}

2、GetAbsDays(MyDate1)- SignDialog::GetAbsDays(MyDate2);


三、通过时间戳计算时间差

1、获取当前时间(秒)

time_t t = time(0);

2、保存时间
Data data;
data.copy((unsigned char *) &t,sizeof(t));
UserDefault::getInstance()->setDataForKey("time_1",data);

3、取时间

auto d = UserDefault::getInstance()->getDataForKey("time_1",Data());
time_t lastExitTime = *((time_t*)d.getBytes());
time_t currentTime= time(0);
time_t time=currentTime-lastExitTime;

4、换算为分钟

int k=time/60;

猜你在找的Cocos2d-x相关文章