bcb 计算时间,日期型函数

 //---------------------------------------------------------------------------
// 使用说明:
//      String  destDateTime;
//      String  str1,str2;
//      fnGetSpecifyDateTime(0,str1,destDateTime,0);
//      str2 = destDateTime;
// 参数说明:
//     iMode  0:trunc(sourceDateTime),获取当天0点
//            1:trunc(sourceDateTime)+1,获取第2天0点
//            2:trunc(sourceDateTime)+1-1/24/60/60,获取当天23:59:59
//            3:获取sourceDateTime对应的当月末时间
//            4:获取sourceDateTime对应的下月初时间
//            5:获取sourceDateTime+incres对应的时间(incres支持正负数,单位为秒)
//            6:获取sourceDateTime+incres对应的时间(incres支持正负数,单位为天)
//            7:获取sourceDateTime+incres对应的时间的当天末时间(incres支持正负数,单位为天)
//            8:获取sourceDateTime+incres对应的时间(incres支持正负数,单位为月)
//            9:获取sourceDateTime+incres对应的时间的当月末时间(incres支持正负数,单位为月)
//         其他:原值sourceDateTime返回
//     sourceDateTime  原时间(格式:yyyy-mm-dd hh:nn:ss)
//     destDateTime    返回时间
//     incres          间隔数
//---------------------------------------------------------------------------
void __fastcall fnGetSpecifyDateTime(int iMode,String sourceDateTime,String &destDateTime,int incres)
{
    if(iMode==0) //trunc(sourceDateTime),获取当天0点
    {
        destDateTime = FormatDateTime("yyyy-mm-dd", StrToDate(StrToDate(sourceDateTime.SubString(1,10)))) + " 00:00:00";
    }
    else if(iMode==1) //trunc(sourceDateTime)+1,获取第2天0点
    {
        destDateTime = FormatDateTime("yyyy-mm-dd", StrToDate(StrToDate(sourceDateTime.SubString(1,10))+1)) + " 00:00:00";
    }
    else if(iMode==2) //trunc(sourceDateTime)+1-1/24/3600,获取当天23:59:59
    {
        destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", StrToDateTime(sourceDateTime.SubString(1,10) + " 23:59:59"));
    }
    else if(iMode==3) //获取sourceDateTime对应的月末时间
    {
        //jianghp@20051218 modify
        //destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", IncMonth(StrToDateTime(DateToStr(StrToDate(sourceDateTime.SubString(1,8)+"01")-1) + " 23:59:59"),1));
        destDateTime = FormatDateTime("yyyy-mm-dd", IncMonth(StrToDate(sourceDateTime.SubString(1,8)+"01"),1)-1) + " 23:59:59";
    }
    else if(iMode==4) //获取sourceDateTime对应的下月初时间
    {
        destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", IncMonth(StrToDateTime(DateToStr(StrToDate(sourceDateTime.SubString(1,8)+"01")) + " 00:00:00"),1));
    }
    else if(iMode==5) //获取sourceDateTime+incres对应的时间(incres支持正负数,单位为秒)
    {
        int Year, Month, Day, Hour, Min, Sec, MSec;
        bool bLeapYear;

        Year =  StrToInt(sourceDateTime.SubString(1,4));
        bLeapYear = IsLeapYear(Year);
        Month = StrToInt(sourceDateTime.SubString(6,2));
        Day = StrToInt(sourceDateTime.SubString(9,2));
        Hour = StrToInt(sourceDateTime.SubString(12,2));
        Min = StrToInt(sourceDateTime.SubString(15,2));
        Sec = StrToInt(sourceDateTime.SubString(18,2));
        Sec += incres;
        while(Sec >= 60)
        {
            Min += 1;
            Sec -= 60;
            if(Min == 60)
            {
              Hour += 1;
              Min = 0;
            }
            if(Hour == 24)
            {
              Day += 1;
              Hour = 0;
            }
            //闰年2月有29天
            if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
            {
                if(Day == 32)
                {
                  Month += 1;
                  Day = 1;
                }
            }
            else
            {
                if(Month == 2)
                {
                    if(bLeapYear)
                    {
                        if(Day == 30)
                        {
                          Month += 1;
                          Day = 1;
                        }
                    }
                    else
                    {
                        if(Day == 29)
                        {
                          Month += 1;
                          Day = 1;
                        }
                    }
                }
                else
                {
                    if(Day == 31)
                    {
                      Month += 1;
                      Day = 1;
                    }
                }
            }
            if(Month == 13)
            {
              Year += 1;
              Month = 1;
            }
        }
        while(Sec < 0)
        {
            Min -= 1;
            Sec += 60;
            if(Min < 0)
            {
              Hour -= 1;
              Min += 60;
            }
            if(Hour < 0)
            {
              Day -= 1;
              Hour += 24;
            }
            if(Day == 0)
            {
                Month -= 1;
                //闰年2月有29天
                if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
                {
                  Day = 31;
                }
                else
                {
                    if(Month == 2)
                    {
                        if(bLeapYear)
                        {
                          Day = 29;
                        }
                        else
                        {
                          Day = 28;
                        }
                    }
                    else
                    {
                      Day = 30;
                    }
                }
            }
            if(Month == 0)
            {
              Year -= 1;
              Month += 12;
            }
        }
        MSec = 0;
        TDateTime dtPresent;
        dtPresent = EncodeDate(Year, Month, Day);
        dtPresent += EncodeTime(Hour, Min, Sec, MSec);
        destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss",dtPresent);
    }
    else if(iMode==6) //获取sourceDateTime+incres对应的时间(incres支持正负数,单位为天)
    {
        destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", StrToDateTime(sourceDateTime)+incres);
    }
    else if(iMode==7) //获取sourceDateTime+incres对应的当天末时间(incres支持正负数,单位为天)
    {
        destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", StrToDateTime(sourceDateTime)+incres);
        destDateTime = destDateTime.SubString(1,10) + " 23:59:59";
    }
    else if(iMode==8) //获取sourceDateTime+incres对应的时间(incres支持正负数,单位为月)
    {
        destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", IncMonth(StrToDateTime(sourceDateTime),incres));
    }
    else if(iMode==9) //获取sourceDateTime+incres对应的当月末时间(incres支持正负数,单位为月)
    {
        //jianghp@20051218 modify
        destDateTime = FormatDateTime("yyyy-mm-dd", IncMonth(StrToDate(sourceDateTime.SubString(1,8)+"01"),incres+1)-1) + " 23:59:59";
        //destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", IncMonth(StrToDateTime(sourceDateTime),incres));
        //destDateTime = FormatDateTime("yyyy-mm-dd hh:nn:ss", IncMonth(StrToDateTime(DateToStr(StrToDate(destDateTime.SubString(1,8)+"01")-1) + " 23:59:59"),1));
    }
    else
    {
        destDateTime = sourceDateTime;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值