C++ MFC 获取当前日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6917607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:53:45  来源:igfitidea点击:

C++ MFC Get current date and time

c++datemfctime

提问by Adam

I've been programming in VB.NET for most of my very programming career. I have a C++ project provided to me, which I need to make a few modifications, and I am feeling hopelessly lost and confused.

在我的大部分编程生涯中,我一直在使用 VB.NET 进行编程。我有一个 C++ 项目提供给我,我需要对其进行一些修改,我感到绝望和迷茫。

It is a Visual Studio 2008 MFC project in C++.

它是一个 C++ 中的 Visual Studio 2008 MFC 项目。

an output variable has been defined:

定义了一个输出变量:

char szout[900];


This line below, is used to append values to the output variable before output:

下面的这一行用于在输出之前将值附加到输出变量:

strcpy(szout, "TextHere")


So one of the many examples from searching, that i have tried, was to include at the top:

因此,我尝试过的众多搜索示例之一是将其包含在顶部:

#include <windows.h>


And then for my code:

然后对于我的代码:

SYSTEMTIME st;
GetSystemTime(&st);
char myDate[20] = st;
CT2CA outputDate(myDate);
strcat(szout, outputDate);


For some reason the variables appended on to szout must be of type CT2CA, which i'm not really sure what this is either.

出于某种原因,附加到 szout 的变量必须是 CT2CA 类型,我也不确定这是什么。

But then I get the following errors on the second and third line (char myDate...etc...) and (CT2CA output....etc....)

但是随后我在第二行和第三行(char myDate...等...)和(CT2CA 输出......等...)上收到以下错误

error C2440: 'initializing' : cannot convert from 'SYSTEMTIME' to 'char [20]'

error C2664: 'ATL::CW2AEX<>::CW2AEX(LPCWSTR) throw(...)' : cannot convert parameter 1 from 'char [20]' to 'LPCWSTR'


So I'll clarify, I am a complete novice with this, and would appreciate any and all help.

所以我要澄清一下,我是一个完整的新手,并希望得到任何和所有帮助。

Thank you,

谢谢,

回答by Chad

If you are using MFC, why not:

如果您使用的是 MFC,为什么不:

// uses printf() format specifications for time
CString t = CTime::GetCurrentTime().Format("%H:%M");

// Or, if you have OLE Support
CString t = COleDateTime::GetCurrentTime().Format("%H:%M");

回答by Pabitra Dash

In MFC the following code is for current date in MMDDYYYY format.

在 MFC 中,以下代码用于 MMDDYYYY 格式的当前日期。

CTime t = CTime::GetCurrentTime();
CString s = t.Format("%m%d%Y");