如何从当前区域设置的短日期格式字符串中的C / C ++?

问题描述 投票:3回答:1

下面的代码(在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 /%米/%Y”的日期格式,虽然“%C”已经被指定。

有没有一种方法我可以格式字符串本身,即“%d /%M /%Y”对于一个给定的语言环境?

c windows date
1个回答
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?

© www.soinside.com 2019 - 2024. All rights reserved.