欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

已知两个时间戳判断这两个时间是否在同一天

程序员文章站 2024-03-20 21:27:40
...

已知两个时间戳判断这两个时间是否在同一天

int Same_Day()
{   
    char Time1[11] = "1516766666";
    char Time2[11] = "1516788888";
	
  time_t Timestamp1,Timestamp2;  
  struct tm *p1,*p2;
  char Day1[9]="";
  char Hour1[3]="";
  Timestamp1=atoi(Time1) + 28000;  
  p1=gmtime(&Timestamp1);
  strftime(Day1, sizeof(date1),"%Y%m%d", p1);
  strftime(Hour1, sizeof(Hour1),"%H", p1);
  
  char Day2[9]="";
  char Hour2[3]="";
  Timestamp2=atoi(Time2) + 28000;
  p2=gmtime(&Timestamp2);
  strftime(Day2, sizeof(Day2),"%Y%m%d", p2);
  strftime(Hour2, sizeof(Hour2),"%H", p2);

  if(atoi(Day1)==atoi(Day2)&&atoi(Hour1)<=23)
  {
    printf("在同一天");
    return 1;
  }
  else
  {
    printf("不在同一天");
    return 0;
  }
}

void main(void)
{
    Same_Day();
}