Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中将日期时间格式从yyyy-MM-dd转换为yyyy/MM/dd+;?_C++_Date_Datetime_Formatting - Fatal编程技术网

C++ 如何在C+中将日期时间格式从yyyy-MM-dd转换为yyyy/MM/dd+;?

C++ 如何在C+中将日期时间格式从yyyy-MM-dd转换为yyyy/MM/dd+;?,c++,date,datetime,formatting,C++,Date,Datetime,Formatting,全部 我对C++很陌生。< /P> 这是我的密码 #include <winnls.h> #include <winnt.h> SYSTEMTIME create_local_time; GetDateFormat( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &create_local_time, NULL, m_szCreationTime.GetBuffer(128), 128); #包括 #包括 系统时间创建本地时间

全部

我对C++很陌生。< /P> 这是我的密码

#include <winnls.h>
#include <winnt.h>



SYSTEMTIME create_local_time;

GetDateFormat( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &create_local_time, NULL, m_szCreationTime.GetBuffer(128), 128);
#包括
#包括
系统时间创建本地时间;
GetDateFormat(LOCALE\u USER\u DEFAULT,DATE\u SHORTDATE,&create\u local\u time,NULL,m_szCreationTime.GetBuffer(128),128);
我正在查看中的GetDateFormat函数

m_szCreationTime以字符串形式返回日期,如2013-03-09

我想将此格式更改为2013/03/09

所以,我正在看我给DATE_SHORTDATE的DWORD dwFlags

但是,我仍然找不到我想要的信息

谁能帮我一下吗

编辑

对不起,我真的错过了非常重要的一部分

m_szCreationTime是CString类型。

Solution1 您可以将第四个参数
format
设置为
“yyyy/mm/dd”
而不是
NULL
,但第二个参数
dwFlags
必须设置为
0
,因为:

这里我们需要设置
format
参数,因此不能将
dwFlags
设置为
NULL
。您可以参考此API更多信息中的文档:

SYSTEMTIME create_local_time;

TCHAR time[128] = {0};
const TCHAR *format = _T("yyyy/MM/dd");
GetLocalTime(&create_local_time);
GetDateFormat( LOCALE_USER_DEFAULT, 0, &create_local_time, format, time, 128);
上述代码片段可以获得所需格式的时间

解决方案2 您还可以在获取字符串后将
-
替换为
/
,例如

#include <algorithm>
#include <string>

void some_func() {
    std::string s = "2013-03-09";
    std::replace( s.begin(), s.end(), '-', '/'); // replace all '-' to '/'
}
请参阅MSDN解决方案1 您可以将第四个参数
format
设置为
“yyyy/mm/dd”
而不是
NULL
,但第二个参数
dwFlags
必须设置为
0
,因为:

这里我们需要设置
format
参数,因此不能将
dwFlags
设置为
NULL
。您可以参考此API更多信息中的文档:

SYSTEMTIME create_local_time;

TCHAR time[128] = {0};
const TCHAR *format = _T("yyyy/MM/dd");
GetLocalTime(&create_local_time);
GetDateFormat( LOCALE_USER_DEFAULT, 0, &create_local_time, format, time, 128);
上述代码片段可以获得所需格式的时间

解决方案2 您还可以在获取字符串后将
-
替换为
/
,例如

#include <algorithm>
#include <string>

void some_func() {
    std::string s = "2013-03-09";
    std::replace( s.begin(), s.end(), '-', '/'); // replace all '-' to '/'
}
请参阅MSDN

#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串s=“2013-03-09”;
替换(s.begin()、s.end()、“-”、“/”);
不能包含
#包括
#包括
使用名称空间std;
int main()
{
字符串s=“2013-03-09”;
替换(s.begin()、s.end()、“-”、“/”);

没问题。不要抱歉,我只是建议你不要投反对票!:)对不起,我错过了重要的部分。m_szCreationTime是CString类型。当我将代码更改为GetDateFormat(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&create_local_time,L“yyyy/MM/dd”,m_szCreationTime.GetBuffer(128),128).m_szCreationTime返回空字符串。@JoshuaSon,请参考
CString
解决方案的更新答案。@feihu谢谢。这非常有用。没有问题。不要为我刚才的建议感到抱歉,这样你就不会被否决了!:)抱歉,我错过了重要的部分。m_szCreationTime是CString类型。当我将代码更改为GetDateFormat时(LOCALE\u USER\u DEFAULT,DATE\u SHORTDATE,&create\u local\u time,L“yyyy/MM/dd”,m\u szCreationTime.GetBuffer(128),128).m_szCreationTime返回空字符串。@JoshuaSon,请参考
CString
解决方案的更新答案。@feihu谢谢。这非常有用。这与
feihu
answer有何不同?@user3414693很抱歉,我没有看到这个答案。我马上写下了这个代码,这与
feihu
answer?@user34有何不同14693对不起,我没看到答案。我马上就写了代码