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

时间:2019-02-04 14:41:57

标签: 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”。 尽管已指定“%c”,但这对应于“%d /%m /%Y”的日期格式。

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

1 个答案:

答案 0 :(得分:0)

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?