c - 如何从 C/C++ 中的当前语言环境获取短日期格式字符串?

标签 c windows date

以下代码(Windows、C++)设置当前语言环境并检索格式化的短日期字符串,格式根据语言环境的短日期格式“%c”。

  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"%c",timeinfo);

假设这给出了给定日期和语言环境的“31/01/2012”。 这对应于“%d/%m/%Y”的日期格式,尽管已指定“%c”。

有没有办法获取格式字符串本身,即给定语言环境的“%d/%m/%Y”?

最佳答案

langinfo 中有一个调用来做这个

作为一个简单的演示

#include <stdio.h>
#include <langinfo.h>

int main()
{
    printf("%s\n", nl_langinfo( D_FMT ) );
}

如果你的 C 编译器不兼容 POSIX,而我认为 Windows 应该是,那么这个问题的答案可能更像是一个指南

What is the correct way to get a LOCALE_SSHORTDATE that is guaranteed to have a full (4-digit) year number?

关于c - 如何从 C/C++ 中的当前语言环境获取短日期格式字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54518529/

相关文章:

c++ - TFTP数据包

java - 使用允许安装目录选择的 Netbeans 创建 .exe

windows - Docker 桌面安装失败 : one prerequisite is not fulfilled on Windows 10

在C中将毫秒转换为日期

java - 使用 PopUp 进行动态日期验证

c - 取消引用双指针时出现问题

c - 至少两次

c - 加法在 c 中不起作用

windows - Docker 容器在启动后立即关闭

javascript - 如何在 React JS 中实时显示时间和日期?