如何在 C 语言中获取昨天的日期?

发布于 2024-10-12 16:47:54 字数 295 浏览 9 评论 0原文

我想将昨天的日期转换为以下格式的字符:YYYYMMDD(没有斜线点等)。

我正在使用此代码来获取今天的日期:

time_t now;

struct tm  *ts;  
char yearchar[80]; 

now = time(NULL);  
ts = localtime(&now);

strftime(yearchar, sizeof(yearchar), "%Y%m%d", ts);

如何调整此代码以使其生成昨天的日期而不是今天的日期?

非常感谢。

I am wanting to get yesterday's date into a char in the format: YYYYMMDD (with no slashes dots etc.).

I am using this code to get today's date:

time_t now;

struct tm  *ts;  
char yearchar[80]; 

now = time(NULL);  
ts = localtime(&now);

strftime(yearchar, sizeof(yearchar), "%Y%m%d", ts);

How would I adapt this code so that it is generating yesterday's date instead of today's?

Many Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

上课铃就是安魂曲 2024-10-19 16:47:54

mktime() 函数将规范化您传递给它的 struct tm(即,它将像 2020/2/0 这样的超出范围的日期转换为内部日期)范围相当于 2020/1/31) - 所以您需要做的就是这样:

time_t now;
struct tm  *ts;  
char yearchar[80]; 

now = time(NULL);
ts = localtime(&now);
ts->tm_mday--;
mktime(ts); /* Normalise ts */
strftime(yearchar, sizeof(yearchar), "%Y%m%d", ts);

The mktime() function will normalise the struct tm that you pass it (ie. it will convert out-of-range dates like 2020/2/0 into the in-range equivalent 2020/1/31) - so all you need to do is this:

time_t now;
struct tm  *ts;  
char yearchar[80]; 

now = time(NULL);
ts = localtime(&now);
ts->tm_mday--;
mktime(ts); /* Normalise ts */
strftime(yearchar, sizeof(yearchar), "%Y%m%d", ts);
§对你不离不弃 2024-10-19 16:47:54

在一些非常罕见的极端情况下(例如在闰秒期间)可能会

now = now - (60 * 60 * 24)

失败,但应该在 99.999999% 的时间内执行您想要的操作。

how about adding

now = now - (60 * 60 * 24)

Might fail in some VERY rare corner cases (e.g. during leapseconds) but should do what you want 99.999999% of the time.

缱倦旧时光 2024-10-19 16:47:54

只需从 time(NULL); 中减去一天的秒数就可以了。将此行:更改

now = time(NULL);

为:

now = time(NULL) - (24 * 60 * 60);

Simply subtracting one day's worth of seconds from time(NULL); should do. Change this line:

now = time(NULL);

to this:

now = time(NULL) - (24 * 60 * 60);
一抹淡然 2024-10-19 16:47:54

请尝试这个代码

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
 
int main(void)
{
    char yestDt[9];
    time_t now = time(NULL);
    now = now - (24*60*60);
    struct tm *t = localtime(&now);
    sprintf(yestDt,"%04d%02d%02d", t->tm_year+1900, t->tm_mday,t->tm_mon+1);
    printf("Target String: \"%s\"", yestDt);
    return 0;
}

Please try this code

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
 
int main(void)
{
    char yestDt[9];
    time_t now = time(NULL);
    now = now - (24*60*60);
    struct tm *t = localtime(&now);
    sprintf(yestDt,"%04d%02d%02d", t->tm_year+1900, t->tm_mday,t->tm_mon+1);
    printf("Target String: \"%s\"", yestDt);
    return 0;
}
雨轻弹 2024-10-19 16:47:54

你已经很接近了。首先,泰勒的解决方案几乎可以工作——您需要使用(24*60*60*1000),因为 time(3) 返回毫秒。但看看那个struct tm。它包含日期所有组成部分的字段。

更新:该死,我的错误 - time(3) 确实返回秒。我正在考虑再打一个电话。但无论如何还是要看看 struct tm 的内容。

You're pretty close on. First of all, Tyler's solution will almost work -- you need to use (24*60*60*1000) since time(3) returns milliseconds. But have a look at that struct tm. It has fields for all the components of a date.

Update: Damn, my mistake -- time(3) does return seconds. I was thinking of another call. But have a look at the contents of struct tm anyway.

故事与诗 2024-10-19 16:47:54

您可以在将 ts 结构体的内容传递给 strftime 之前对其进行操作。该月的日期包含在 tm_mday 成员中。基本过程:

/**
 * If today is the 1st, subtract 1 from the month
 * and set the day to the last day of the previous month
 */
if (ts->tm_mday == 1)
{
  /**
   * If today is Jan 1st, subtract 1 from the year and set
   * the month to Dec.
   */
  if (ts->tm_mon == 0)
  {
    ts->tm_year--;
    ts->tm_mon = 11;
  }
  else
  {
    ts->tm_mon--;
  }

  /**
   * Figure out the last day of the previous month.
   */
  if (ts->tm_mon == 1)
  {
    /**
     * If the previous month is Feb, then we need to check 
     * for leap year.
     */
    if (ts->tm_year % 4 == 0 && ts->tm_year % 400 == 0)
      ts->tm_mday = 29;
    else
      ts->tm_mday = 28;
  }
  else
  {
    /**
     * It's either the 30th or the 31st
     */
    switch(ts->tm_mon)
    {
       case 0: case 2: case 4: case 6: case 7: case 9: case 11:
         ts->tm_mday = 31;
         break;

       default:
         ts->tm_mday = 30;
    }
  }
}
else
{
  ts->tm_mday--;
}

编辑:是的,一个月中的天数从 1 开始编号,而其他所有内容(秒、分钟、小时、工作日和一年中的天数)从 0 开始编号。

You can manipulate the contents of the ts struct before passing it to strftime. The day of the month is contained in the tm_mday member. Basic procedure:

/**
 * If today is the 1st, subtract 1 from the month
 * and set the day to the last day of the previous month
 */
if (ts->tm_mday == 1)
{
  /**
   * If today is Jan 1st, subtract 1 from the year and set
   * the month to Dec.
   */
  if (ts->tm_mon == 0)
  {
    ts->tm_year--;
    ts->tm_mon = 11;
  }
  else
  {
    ts->tm_mon--;
  }

  /**
   * Figure out the last day of the previous month.
   */
  if (ts->tm_mon == 1)
  {
    /**
     * If the previous month is Feb, then we need to check 
     * for leap year.
     */
    if (ts->tm_year % 4 == 0 && ts->tm_year % 400 == 0)
      ts->tm_mday = 29;
    else
      ts->tm_mday = 28;
  }
  else
  {
    /**
     * It's either the 30th or the 31st
     */
    switch(ts->tm_mon)
    {
       case 0: case 2: case 4: case 6: case 7: case 9: case 11:
         ts->tm_mday = 31;
         break;

       default:
         ts->tm_mday = 30;
    }
  }
}
else
{
  ts->tm_mday--;
}

Edit: Yes, days of the month are numbered from 1 whereas everything else (seconds, minutes, hours, weekdays, and days of the year) are numbered from 0.

み格子的夏天 2024-10-19 16:47:54
time_t now;
int day;

struct tm  *ts;  
char yearchar[80]; 

now = time(NULL);  
ts = localtime(&now);
day = ts->tm_mday;

now = now + 10 - 24 * 60 * 60;
ts = localtime(&now);
if (day == ts->tm_mday)
{
  now = now - 24 * 60 * 60;
  ts = localtime(&now);
}

strftime(yearchar, sizeof(yearchar), "%Y%m%d", ts);

也适用于闰秒。

time_t now;
int day;

struct tm  *ts;  
char yearchar[80]; 

now = time(NULL);  
ts = localtime(&now);
day = ts->tm_mday;

now = now + 10 - 24 * 60 * 60;
ts = localtime(&now);
if (day == ts->tm_mday)
{
  now = now - 24 * 60 * 60;
  ts = localtime(&now);
}

strftime(yearchar, sizeof(yearchar), "%Y%m%d", ts);

Will work with leap seconds too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文