【问题标题】:display date 90 days earlier form CURRENT (ANSI C)显示 90 天前的日期形式 CURRENT (ANSI C)
【发布时间】:2014-05-20 10:39:32
【问题描述】:

有没有一种简单的方法可以计算比当前提前 90 天?例如,如果今天是 5 月 31 日,那么 90 天前的日期是什么?有这样做的功能吗?谢谢

【问题讨论】:

  • 首先你需要决定如何表示日期。您提到了 ANSI C 和 MFC 的某种邪恶的混合。会是哪个?据我所知,MFC是一个C++库,MS编译器甚至不支持ANSI C。

标签: c mfc ansi


【解决方案1】:
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void) {
  time_t t = time(0);                      // NOW
  struct tm tm[1];
  memmove(tm, localtime(&t), sizeof tm);   // convert to struct tm
  tm->tm_mday -= 90;                       // subtract 90 days
  time_t then2 = mktime(tm);               // convert to time_t and normalize
  printf("%s\n", ctime(&then2));
  return 0;
}

【讨论】:

  • +1 这是我将使用的方法,而不是将 NOW 日期/时间设置为中午并仅打印出日期(以避免 DST 问题)。 Doubt OP 已经研究了这种关注程度。虽然也许这段代码对tm_isdst 字段的非调整可以解决这个问题,
【解决方案2】:

可以使用 COleDateTime (link)COleDateTimeSpan (link) 类。

#include <atlcomtime.h>

COleDateTime dt = COleDateTime::GetCurrentTime();
COleDateTimeSpan span(90, 0, 0, 0);
COleDateTime dt2 = dt - span;

【讨论】:

    【解决方案3】:

    不是一个单一的功能,不。

    但是你可以很容易地做到这一点:

    • 使用time()获取当前时间。
    • 减去 90 天的秒数。
    • 使用localtime() 转换为当地时间。
    • 如果需要,使用strftime() 转换为字符串。

    【讨论】:

      【解决方案4】:

      DateTime dt = DateTime.Now.AddDays(-90);

      string edate = dt.ToString("dd-MM-yyyy");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-07
        • 1970-01-01
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多