1、日历的方式
#include<stdio.h>
#include <time.h>
struct{
int y;
int m;
int d;
}jintian;
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
time_t st_now;
struct tm *p_tmp = NULL;
time(&st_now);
p_tmp = localtime(&st_now);
jintian.y = p_tmp->tm_year+1900;
jintian.m = p_tmp->tm_mon+1;
jintian.d = p_tmp->tm_mday;
jintian.d-=1;
if(jintian.d<=0)
{
jintian.m--;
if(jintian.m<=0)
{
jintian.y--;
jintian.m+=12;
}
jintian.d+=m[jintian.m];
if(jintian.m==2&&((jintian.y%4==0 && jintian.y%100!=0) || jintian.y%400==0))
{
jintian.d++;
}
}
printf("昨天是%d年%d月%d日\n",jintian.y,jintian.m,jintian.d);
return 0;
}
2、时间戳的方式
#include <stdio.h>
#include <time.h>
int main()
{
time_t st_now;
time_t yesterday;
struct tm *p_tmp = NULL;
time(&st_now);
yesterday = st_now -24*60*60;
p_tmp = localtime(&st_now);
printf("昨天是 %d年%d月%d日",p_tmp->tm_year+1900,p_tmp->tm_mon+1,p_tmp->tm_mday);
}