【问题标题】:How to assign current system time to a String^ variable?如何将当前系统时间分配给 String^ 变量?
【发布时间】:2015-12-31 06:56:14
【问题描述】:

如何在按钮单击事件中获取当前系统时间并将其分配给(可能)指定格式(如:YYYY-MM-DD-HH-MM-SS)的String^ 变量?

我正在使用 Visual Studio 2013。

【问题讨论】:

  • String^ s = DateTime::Now.ToString("yyyy-MM-dd-HH-mm-ss");
  • @HansPassant 非常感谢!

标签: visual-studio visual-c++ visual-studio-2013 c++-cli visual-c++-2013


【解决方案1】:

您可以将time 函数与localtime 函数一起使用。

示例代码:

//#include <time.h>
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
//The years are since 1900 according the documentation, so add 1900 to the actual year result.
printf("Date: %d/%d/%d Time: %d:%d:%d",timeinfo->tm_year + 1900, timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

【讨论】:

  • 感谢您的帮助,我真的很感激,但是@HansPassant 的一个班轮在这里似乎是一个更合适的解决方案。
  • 没问题,我没注意到你在使用 C++/CLI :)
猜你喜欢
  • 2012-11-14
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多