下面这个是百度上找到的
CString s("2001-8-31");
CString s1(" 00:00:00");//好像必须是年月日时分秒都有的,并且是这个格式YYYY-MM-DD XX:XX:XX的
s = s+s1;
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime t1(nYear, nMonth, nDate, nHour, nMin, nSec);
CTimeSpan ts(1,0,0,0);//一天,想加两天的话就(2,0,0,0)
t1+= ts;//t1 = 2001-9-1
CString strDay;
strDay.Format("%d-%d-%d",t1.GetYear(),t1.GetMonth(),t1.GetDay() );
s = strDay;
AfxMessageBox(s);//2001-9-1 如果不到10的话它就不会是2001-09-01了
由于我的项目必须严格按照YYYY-MM-DD XX:XX:XX这个格式的,有0也必须带上,所以后面就添加了下面的代码
//下面的可以把天数和月份小与10的前面加0 比如2001-9-8 ------->2001-09-08
if(t1.GetMonth() < 10 && t1.GetDay() < 10)
{
strDay.Format("%d-0%d-0%d",t1.GetYear(),t1.GetMonth(),t1.GetDay());
}
else if(t1.GetMonth() < 10)
{
strDay.Format("%d-0%d-%d",t1.GetYear(),t1.GetMonth(),t1.GetDay());
}
else
{
strDay.Format("%d-%d-0%d",t1.GetYear(),t1.GetMonth(),t1.GetDay());
}
本文详细介绍了如何将不同格式的日期字符串转换为标准日期格式YYYY-MM-DDXX:XX:XX,并提供了处理天数和月份不足10时的格式化方法。
1835

被折叠的 条评论
为什么被折叠?



