現在からn日前の日付を取得する。
timer -= (86400 * n);のところを+にして、未来日も求められるようにしても良いかと思いますが、
未来日を求めるようなシステムは今まで経験したことがありません。
#include <stdio.h>
#include <time.h>
/*****************************************************************************/
/* ymd[9]のサイズで引数を渡すこと */
/*****************************************************************************/
void CalcBeforeDate(int n, char *ymd)
{
time_t timer;
struct tm *t_st;
/* 現在時刻ノ取得 */
time(&timer);
/* 1日の秒数86400秒 * n日を引く */
timer -= (86400 * n);
/* 時刻を構造体に変換 */
t_st = localtime(&timer);
/* 文字列に書き込む */
sprintf(ymd, "%d%02d%02d", t_st->tm_year + 1900
, t_st->tm_mon + 1
, t_st->tm_mday);
}
使い方
int main(void)
{
char ymd[8 + 1];
CalcBeforeDate(1, ymd);
printf("1日前の日付は%sです。\n", ymd);
return 0;
}