コード例 #1
0
        public int TinhSoNgay(string manp)
        {
            ChiTietPhieuNhanPhongData data = new ChiTietPhieuNhanPhongData();
            DataTable tbl   = data.LayChiTietPhieuNhan(manp);
            DateTime  nnhan = Convert.ToDateTime(tbl.Rows[0]["NgayNhan"].ToString());
            DateTime  ntra  = Convert.ToDateTime(tbl.Rows[0]["NgayTraThucTe"].ToString());

            int soNgay = 0;

            if (nnhan.Year == ntra.Year)
            {
                soNgay = ntra.DayOfYear - nnhan.DayOfYear;
            }
            else
            {
                int soNam = ntra.Year - nnhan.Year;
                if (soNam == 1)
                {
                    if ((nnhan.Year % 400 == 0) || (nnhan.Year % 4 == 0 && nnhan.Year % 100 == 0))
                    {
                        soNgay = (366 - nnhan.DayOfYear) + ntra.DayOfYear;
                    }
                    else
                    {
                        soNgay = (365 - nnhan.DayOfYear) + ntra.DayOfYear;
                    }
                }
                else
                {
                    for (int i = 0; i < soNam - 1; i++)
                    {
                        if (((nnhan.Year + i) % 400 == 0) || ((nnhan.Year + i) % 4 == 0 && (nnhan.Year + i) % 100 == 0))
                        {
                            soNgay = soNgay + 366;
                        }
                        else
                        {
                            soNgay = soNgay + 365;
                        }
                    }
                    if ((nnhan.Year % 400 == 0) || (nnhan.Year % 4 == 0 && nnhan.Year % 100 == 0))
                    {
                        soNgay = (366 - nnhan.DayOfYear) + ntra.DayOfYear;
                    }
                    else
                    {
                        soNgay = (365 - nnhan.DayOfYear) + ntra.DayOfYear;
                    }
                }
            }
            if (soNgay == 0)
            {
                soNgay = 1;
            }

            numSoNgay.Value = soNgay;
            return(soNgay);
        }