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, 30, 31, 31, 30, 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;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值