計算天數,時數

  • 27289
  • 0
  • C#
  • 2011-04-22

摘要:計算天數,時數

public string cal(int checksum)//checksum=20081022125525(年月日時分秒)
    {
        int temp;
        int day1 = checksum % 100;
        temp = checksum / 100;
        int month1 = temp % 100;
        int year1 = temp / 100;
        //以上是將checksum的日期分離出來

        DateTime dt = DateTime.Now;
        int day2 = dt.Day;
        int month2 = dt.Month;
        int year2 = dt.Year;
        //以上是取得當下系統時間

        if (month1 <= 2)
        {
            year1 = year1 - 1;
            month1 = month1 + 13;
        }
        else
        {
            month1 = month1 + 1;
        }
        if (month2 <= 2)
        {
            year2 = year2 - 1;
            month2 = month2 + 13;
        }
        else
        {
            month2 = month2 + 1;
        }
        //以上為年月的判斷式,為了實現下面的公式的前置工作

        int N1 = 1461 * year1 / 4 + 153 * month1 / 5 + day1;
        int N2 = 1461 * year2 / 4 + 153 * month2 / 5 + day2;
        int daycount = N2 - N1;

        //daycount就是相差的天數哩!!
        return daycount.ToString();
    }
    
//第二版

    public string cal_days(string start_date, string end_date)//date=20081022(年月日)
    {

        int year1 = int.Parse(start_date.ToString().Substring(0, 4));
        int month1 = int.Parse(start_date.ToString().Substring(4, 2));
        int day1 = int.Parse(start_date.ToString().Substring(6, 2));
        //以上是將start_date的日期分離出來

        int year2 = int.Parse(end_date.ToString().Substring(0, 4));
        int month2 = int.Parse(end_date.ToString().Substring(4, 2));
        int day2 = int.Parse(end_date.ToString().Substring(6, 2));
        //以上是將end_date的日期分離出來

        if (month1 <= 2)
        {
            year1 = year1 - 1;
            month1 = month1 + 13;
        }
        else
        {
            month1 = month1 + 1;
        }
        if (month2 <= 2)
        {
            year2 = year2 - 1;
            month2 = month2 + 13;
        }
        else
        {
            month2 = month2 + 1;
        }
        //以上為年月的判斷式,為了實現下面的公式的前置工作

        int N1 = 1461 * year1 / 4 + 153 * month1 / 5 + day1;
        int N2 = 1461 * year2 / 4 + 153 * month2 / 5 + day2;
        int daycount = N2 - N1+1;

        //daycount就是相差的天數
        return daycount.ToString();
    }

    //計算時間 0000-2400 (15分鐘為單位) 搭配第二版計算天數使用 (跨日計算)
    public decimal cal_hours(string start_hour, string end_hour)
    {
        try
        {
            int days = int.Parse(cal_days(txtStart_Date.Text.Trim(), txtEnd_Date.Text.Trim()));
            decimal h1 = decimal.Parse(start_hour.Substring(0, 2));
            decimal m1 = decimal.Parse(start_hour.Substring(2, 2));
            decimal h2 = decimal.Parse(end_hour.Substring(0, 2));
            decimal m2 = decimal.Parse(end_hour.Substring(2, 2));

            if ( days == 1)
            {
                //非跨日計算


                decimal hh = h2 - h1;
                if (m1 > m2)
                {
                    hh--;
                    m2 += 60;
                }
                decimal mm = Math.Abs(m2 - m1) / 60;


                return hh + mm;
            }
            else if (days == 2)
            {
                //跨日計算
                //1.計算至2400時數
                decimal hh1 = 24 - h1;
                if (m1 > m2)
                {
                    hh1--;
                    m2 += 60;
                }
                //decimal mm1 = Math.Abs(m2 - m1) / 60;
                //hh1 = hh1 + mm1;

                //2.計算2400至結束時間時數
                decimal hh2 = h2;
                if (m1 > m2)
                {
                    hh2--;
                    m2 += 60;
                }
                decimal mm2 = Math.Abs(m2 - m1) / 60;
                hh2 = hh2 + mm2;
                return hh1 + hh2;
            }
            else
            {
                ((DropDownList)Master.FindControl("ddlMSG")).Items.Insert(0, "起迄日不得相差超過兩天");
            }

        }
        catch { ((DropDownList)Master.FindControl("ddlMSG")).Items.Insert(0, "cal_hours"); }
        return 0;
    }