时间周期的实现方法

为了实现一个工资周期计算,使用指定月上下月作为周期

1、起始类型

 

 

2、起始日

 

3、结束日期和结束日与起始相似

4、几个相关函数

/// <summary>
/// 根据核算周期字符串获取对应的Array
/// 如果异常或者非法字符串,转换为默认的当月初~当月末形式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int[] GetHeSuanZhouQiArray(string str)
{
    try
    {
        string[] strSplit = str.Split(',');
        if (strSplit.Length == 4)
        {
            string strStartType = strSplit[0];
            string strStartMonthDay = strSplit[1];
            string strEndType = strSplit[2];
            string strEndMonthDay = strSplit[3];

            int.TryParse(strStartType, out int iStartType);
            int.TryParse(strStartMonthDay, out int iStartMonthDay);
            int.TryParse(strEndType, out int iEndType);
            int.TryParse(strEndMonthDay, out int iEndMonthDay);

            if (iStartType < 0 || iStartType > 1)
            {
                iStartType = 1;
            }
            if (iStartMonthDay < 0 || iStartMonthDay > 32)
            {
                iStartMonthDay = 0;
            }
            if (iEndType < 0 || iEndType > 2)
            {
                iEndType = 1;
            }
            if (iEndMonthDay < 0 || iEndMonthDay > 32)
            {
                iEndMonthDay = 32;
            }
            return new int[] { iStartType, iStartMonthDay, iEndType, iEndMonthDay };
        }
        else return new int[] { 1, 0, 1, 32 };
    }
    catch (Exception ex)
    {
        return new int[] { 1, 0, 1, 32 };
    }

}
/// <summary>
/// 根据核算周期获取说明信息
/// 比如1,1,1,30转换为当月1号-当月30号
/// </summary>
/// <param name="hesuanzhouqi"></param>
/// <returns></returns>
public static string GetHeSuanZhouQiInfo(string hesuanzhouqi)
{
    int[] heSuanZhouQiArray = BizDo.GetHeSuanZhouQiArray(hesuanzhouqi);
    int iStartType = heSuanZhouQiArray[0];
    string strStartType = "";
    int iStartMonthDay = heSuanZhouQiArray[1];
    string strStartMonthDay = "";
    int iEndType = heSuanZhouQiArray[2];
    string strEndType = "";
    int iEndMonthDay = heSuanZhouQiArray[3];
    string strEndMonthDay = "";
    //起始类型
    if (iStartType == 0)
    {
        strStartType = "前月";
    }
    else if (iStartType == 1)
    {
        strStartType = "当月";
    }
    else if (iStartType == 2)
    {
        strStartType = "次月";
    }
    //结束类型
    if (iEndType == 0)
    {
        strEndType = "前月";
    }
    else if (iEndType == 1)
    {
        strEndType = "当月";
    }
    else if (iEndType == 2)
    {
        strEndType = "次月";
    }

    //起始月份
    if (iStartMonthDay == 0)
    {
        strStartMonthDay = "月初";
    }
    else if (iStartMonthDay == 32)
    {
        strStartMonthDay = "月末";
    }
    else
    {
        strStartMonthDay = iStartMonthDay + "";
    }
    //结束月份
    if (iEndMonthDay == 0)
    {
        strEndMonthDay = "月初";
    }
    else if (iEndMonthDay == 32)
    {
        strEndMonthDay = "月末";
    }
    else
    {
        strEndMonthDay = iEndMonthDay + "";
    }
    //throw new NotImplementedException();

    string strAll = strStartType + strStartMonthDay + "~" + strEndType + strEndMonthDay;
    return strAll;
}

/// <summary>
/// 计算实际周期
/// </summary>
/// <param name="heSuanZhouQi">核算周期</param>
/// <param name="heSuanYueFen">核算月份</param>
/// <param name="startYearMonthDay">用于返回的开始年月日</param>
/// <param name="endYearMonthDay">用于返回的结束年月日</param>
public static void CalcShiJiZhouQi(string heSuanZhouQi, string heSuanYueFen, out DateTime startYearMonthDay, out DateTime endYearMonthDay)
{
    DateTime.TryParse(heSuanYueFen, out DateTime dateTimeHeSuanYueFen);
    int[] heSuanArray = BizDo.GetHeSuanZhouQiArray(heSuanZhouQi);

    int iStartType = heSuanArray[0];
    int iStartMonthDay = heSuanArray[1];
    int iEndType = heSuanArray[2];
    int iEndMonthDay = heSuanArray[3];
    startYearMonthDay = dateTimeHeSuanYueFen;
    endYearMonthDay = dateTimeHeSuanYueFen;
    //1、计算起始月份
    if (iStartType == 0)//0:前月;1:当月;2;次月
    {
        startYearMonthDay = startYearMonthDay.AddMonths(-1);
    }
    else if (iStartType == 1)
    {
        //不用处理
    }
    else if (iStartType == 2)
    {
        //不用处理;
    }
    int finalStartDay = startYearMonthDay.AddMonths(1).AddDays(-1).Day;//当月总天数
    //2、计算起始月天数
    if (iStartMonthDay <= 1)
    {
        startYearMonthDay = new DateTime(startYearMonthDay.Year, startYearMonthDay.Month, 1);
    }
    else if (iStartMonthDay >= finalStartDay)
    {
        startYearMonthDay = new DateTime(startYearMonthDay.Year, startYearMonthDay.Month, finalStartDay);
    }
    else
    {
        startYearMonthDay = new DateTime(startYearMonthDay.Year, startYearMonthDay.Month, iStartMonthDay);
    }

    //3、计算结束月份
    if (iEndType == 0)//0:前月;1:当月;2;次月
    {
        endYearMonthDay = endYearMonthDay.AddMonths(-1);
    }
    else if (iEndType == 1)
    {
        //不用处理
    }
    else if (iEndType == 2)
    {
        endYearMonthDay = endYearMonthDay.AddMonths(1);
    }

    //4、计算结束月天数
    int finalEndDay = endYearMonthDay.AddMonths(1).AddDays(-1).Day;//当月总天数
    if (iEndMonthDay <= 1)
    {
        endYearMonthDay = new DateTime(endYearMonthDay.Year, endYearMonthDay.Month, 1);
    }
    else if (iEndMonthDay >= finalEndDay)
    {
        endYearMonthDay = new DateTime(endYearMonthDay.Year, endYearMonthDay.Month, finalEndDay);
    }
    else
    {
        endYearMonthDay = new DateTime(endYearMonthDay.Year, endYearMonthDay.Month, iEndMonthDay);
    }
}
       //获取核算周期字符串
        //0月初,32月末,其他1~31号
        private string GetHeSuanZhouQiStr()
        {
            string strStartType = cbStartType.Value.ToString();
            string strStartMonthDay = cbStartMonthDay.Value.ToString();
            string strEndType = cbEndType.Value.ToString();
            string strEndMonthDay = cbEndMonthDay.Value.ToString();

            int.TryParse(strStartType, out int iStartType);
            int.TryParse(strStartMonthDay, out int iStartMonthDay);
            int.TryParse(strEndType, out int iEndType);
            int.TryParse(strEndMonthDay, out int iEndMonthDay);

            if (iStartType < 0 || iStartType > 1)
            {
                iStartType = 1;
            }
            if (iStartMonthDay < 0 || iStartMonthDay > 32)
            {
                iStartMonthDay = 0;
            }
            if (iEndType < 0 || iEndType > 2)
            {
                iEndType = 1;
            }
            if (iEndMonthDay < 0 || iEndMonthDay > 32)
            {
                iEndMonthDay = 32;
            }
            string strRet = "";
            if (iStartType > iEndType)
            {
                strRet = "error:" + "核算周期起始月份不能大于结束月份";
            }
            else if (iStartType == iEndType)
            {
                if (iStartMonthDay >= 31) iStartMonthDay = 31;
                if (iStartMonthDay ==0) iStartMonthDay = 1;
                if (iEndMonthDay >= 31) iEndMonthDay = 31;
                if (iEndMonthDay ==0) iEndMonthDay = 1;
                //转换为1~31天进行比较
                if ((iStartMonthDay > iEndMonthDay))
                {
                    strRet = "error:" + "核算周期在一个月,起始日不能大于结束日";
                }
                else
                {
                    strRet = strStartType + "," + strStartMonthDay + "," + strEndType + "," + strEndMonthDay;
                }
            }
            else
            {
                strRet=strStartType + "," + strStartMonthDay + "," + strEndType + "," + strEndMonthDay;
            }
            return strRet;
        }

 

5、前段代码,使用ext.net,可以改成自己的html

<table>
    <tr>
        <td>
            <ext:ComboBox runat="server" Width="75px" SelectedIndex="1"
                <Items>
                    <ext:ListItem Text="前月" Value="0" />
                    <ext:ListItem Text="当月"  Value="1" />
                </Items>
            </ext:ComboBox>
        </td>
        <td>
            <ext:ComboBox runat="server" Width="75px"  SelectedIndex="0
                <Items>
                    <ext:ListItem Text="月初" Value="0" />
                    <ext:ListItem Text="月末" Value="32" />
                    <ext:ListItem Text="1号" Value="1" />
                    <ext:ListItem Text="2号" Value="2" />
                    <ext:ListItem Text="3号" Value="3" />
                    <ext:ListItem Text="4号" Value="4" />
                    <ext:ListItem Text="5号" Value="5" />
                    <ext:ListItem Text="6号" Value="6" />
                    <ext:ListItem Text="7号" Value="7" />
                    <ext:ListItem Text="8号" Value="8" />
                    <ext:ListItem Text="9号" Value="9" />
                    <ext:ListItem Text="10号" Value="10" />
                    <ext:ListItem Text="11号" Value="11" />
                    <ext:ListItem Text="12号" Value="12" />
                    <ext:ListItem Text="13号" Value="13" />
                    <ext:ListItem Text="14号" Value="14" />
                    <ext:ListItem Text="15号" Value="15" />
                    <ext:ListItem Text="16号" Value="16" />
                    <ext:ListItem Text="17号" Value="17" />
                    <ext:ListItem Text="18号" Value="18" />
                    <ext:ListItem Text="19号" Value="19" />
                    <ext:ListItem Text="20号" Value="20" />
                    <ext:ListItem Text="21号" Value="21" />
                    <ext:ListItem Text="22号" Value="22" />
                    <ext:ListItem Text="23号" Value="23" />
                    <ext:ListItem Text="24号" Value="24" />
                    <ext:ListItem Text="25号" Value="25" />
                    <ext:ListItem Text="26号" Value="26" />
                    <ext:ListItem Text="27号" Value="27" />
                    <ext:ListItem Text="28号" Value="28" />
                    <ext:ListItem Text="29号" Value="29" />
                    <ext:ListItem Text="30号" Value="30" />
                    <ext:ListItem Text="31号" Value="31" />
                </Items>
            </ext:ComboBox>
        </td>
        <td>~</td>
        <td style="background-color: white;" class="tddeptcss">
            <ext:ComboBox runat="server" Width="75px"  SelectedIndex="1
                <Items>
                    <ext:ListItem Text="前月" Value="0" />
                    <ext:ListItem Text="当月" Value="1" />
                    <ext:ListItem Text="次月" Value="2" />
                </Items>
            </ext:ComboBox>
        </td>
        <td>
            <ext:ComboBox runat="server" Width="75px"  SelectedIndex="1
                <Items>
                    <ext:ListItem Text="月初" Value="0" />
                    <ext:ListItem Text="月末" Value="32" />
                    <ext:ListItem Text="1号" Value="1" />
                    <ext:ListItem Text="2号" Value="2" />
                    <ext:ListItem Text="3号" Value="3" />
                    <ext:ListItem Text="4号" Value="4" />
                    <ext:ListItem Text="5号" Value="5" />
                    <ext:ListItem Text="6号" Value="6" />
                    <ext:ListItem Text="7号" Value="7" />
                    <ext:ListItem Text="8号" Value="8" />
                    <ext:ListItem Text="9号" Value="9" />
                    <ext:ListItem Text="10号" Value="10" />
                    <ext:ListItem Text="11号" Value="11" />
                    <ext:ListItem Text="12号" Value="12" />
                    <ext:ListItem Text="13号" Value="13" />
                    <ext:ListItem Text="14号" Value="14" />
                    <ext:ListItem Text="15号" Value="15" />
                    <ext:ListItem Text="16号" Value="16" />
                    <ext:ListItem Text="17号" Value="17" />
                    <ext:ListItem Text="18号" Value="18" />
                    <ext:ListItem Text="19号" Value="19" />
                    <ext:ListItem Text="20号" Value="20" />
                    <ext:ListItem Text="21号" Value="21" />
                    <ext:ListItem Text="22号" Value="22" />
                    <ext:ListItem Text="23号" Value="23" />
                    <ext:ListItem Text="24号" Value="24" />
                    <ext:ListItem Text="25号" Value="25" />
                    <ext:ListItem Text="26号" Value="26" />
                    <ext:ListItem Text="27号" Value="27" />
                    <ext:ListItem Text="28号" Value="28" />
                    <ext:ListItem Text="29号" Value="29" />
                    <ext:ListItem Text="30号" Value="30" />
                    <ext:ListItem Text="31号" Value="31" />
                </Items>
            </ext:ComboBox>
        </td>
    </tr>
</table>

 

posted @ 2019-12-20 11:09  zhaogaojian  阅读(390)  评论(0编辑  收藏  举报