对日期类型的操作问题??????????????????????????????????????

xinfs 2003-12-13 11:15:28
1.如何实现DELPHI的 INCDay函数的功能

例如IncDay(strtodatetime('2003-4-3',3)


日期加3天,返回"2003-04-06".



要求在VC中返回Coledatetime类型

-----------------------------


2. 如何实现DELPHI的 IncMonth函数的功能

例如 IncMonth(strtodatetime('2003-12-3',2)


日期加2个月,返回"2004-02-03" .

要求在VC中返回Coledatetime类型

-----------------------------

3.如何实现DELPHI的 monthsbetween函数的功能

monthsbetween( strtodate('2003-4-1'),strtodate('2003-6-1'))

返回2个月



请给出详细的代码,急
...全文
26 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Earthdog 2003-12-14
  • 打赏
  • 举报
回复
1.
COleDateTime odt1(2003, 12, 14);
COleDateTime odt2 = odt1 + COleDateTimeSpan(2);
// odt2 now is 2003-12-16

2.
BOOL IsLeapYear(DATE dt)
{
SYSTEMTIME st;
::VariantTimeToSystemTime(dt, &st)

return IsLeapYear(st.wYear);
}

BOOL IsLeapYear(WORD wYear)
{
return (wYear % 400 == 0 || wYear % 4 == 0 && wYear % 100 != 0);
}

DATE IncMonth(DATE dt, WORD wIncrement)
{
SYSTEMTIME st;
::VariantTimeToSystemTime(dt, &st);

if (wIncrement >= 0)
{
st.wMonth += wIncrement;
st.wYear = st.wMonth / 12;
st.wMonth %= 12;
}
else
{
st.wMonth += wIncrement;
while (st.wMonth <= 0)
{
st.wMonth += 12;
st.wYear--;
}
}

swith (st.wMonth)
{
case 4:
case 6:
case 9:
case 11:
{
if (st.wDay > 30)
{
st.wDay = 30;
}
}
break;
case 2:
{
if (IsLeapYear(st.wYear))
{
if (st.wDay > 29)
{
st.wDay = 29;
}
}
else
{
if (st.wDay > 28)
{
st.wDay = 28;
}
}
}
break;
default:
break;
}

::SystemTimeToVariantTime(&st, &dt);

return dt;
}

COleDateTime odt1(2003, 12, 14);
COleDateTime odt2 = IncMonth(odt1, 3);

3.
int MonthBetween(DATE dt1, DATE dt2)
{
SYSTEMTIME st1, st2;
::VariantTimeToSystemTime(dt1, &st1);
::VariantTimeToSystemTime(dt2, &st2);

int nTotalMonth1 = st1.wYear * 12 + st1.wMonth;
int nTotalMonth2 = st2.wYear * 12 + st2.wMonth;

return nTotalMonth2 - nTotalMonth1;
}
carbon107 2003-12-14
  • 打赏
  • 举报
回复
还有减法运算符
carbon107 2003-12-14
  • 打赏
  • 举报
回复
COleDateTime Time(year, month, day, Hour, Minute, Second);
每个参数的定义
运用COleDateTime的加法操作符运算, 你的其它两个问题也是如此
carbon107 2003-12-14
  • 打赏
  • 举报
回复

COleDateTime Time1(2003, 4, 3, 0, 0, 0);
COleDateTime Time2(0, 0, 3, 0, 0, 0);
COleDateTime Time = Time1 + Time2;

xinfs 2003-12-14
  • 打赏
  • 举报
回复

我按照楼上" carbon107" 做法

CString str;
COleDateTime Time1(2003, 4, 3, 0, 0, 0);
COleDateTime Time2(0, 0, 3, 0, 0, 0);
COleDateTime Time3 = Time1 + Time2;
//转成字符串
str=Time3.Format("%Y/%m/%d" );


AfxMessageBox(str);


编译时没有错误,但运行时出错,为什么????????????
Earthdog 2003-12-14
  • 打赏
  • 举报
回复
COleDateTime odt1(2003, 4, 3, 0, 0, 0);
COleDateTimeSpan odts(3, 0, 0, 0);
COleDateTime odt2 = odt1 + odts;
str = odt2.Format(_T("%Y-%m-%d"));
AfxMessageBox(str);


你还是参见我上面给你的那些代码吧,都是可以用的,而且对于后两个问题都是提供的函数,对于使用DATE的地方,你直接使用COleDateTime就可以了
xinfs 2003-12-14
  • 打赏
  • 举报
回复
大佬呀,还是不行呀,请给出详细的代码呀!!!!!!!!!!!!!

下面代码也是出错!!!


CString str;
COleDateTime Time1(2003, 4, 3, 0, 0, 0);
COleDateTime Time2(0, 0, 3, 0, 0, 0);
COleDateTimeSpan Time3 = Time1 + Time2;
str=Time3.Format("%Y/%m/%d" );


AfxMessageBox(str);
Earthdog 2003-12-14
  • 打赏
  • 举报
回复
COleDateTime并没有定义有两个COleDateTime类型的变量相加的功能,所以会出错,你应该使用COleDateTime加上COleDateTimeSpan的功能来实现
fsxin 2003-12-14
  • 打赏
  • 举报
回复

我按照楼上" carbon107" 做法

CString str;
COleDateTime Time1(2003, 4, 3, 0, 0, 0);
COleDateTime Time2(0, 0, 3, 0, 0, 0);
COleDateTime Time3 = Time1 + Time2;
//转成字符串
str=Time3.Format("%Y/%m/%d" );


AfxMessageBox(str);


编译时没有错误,但运行时出错,为什么????????????
crystal_heart 2003-12-13
  • 打赏
  • 举报
回复
up
carbon107 2003-12-13
  • 打赏
  • 举报
回复
非常的简单
DebugXP 2003-12-13
  • 打赏
  • 举报
回复
关猪中。。。。
学习up

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧