如何检查第一段时间是否包含另一段时间(第二段时间为6小时或更长时间) [英] How Can I Check That First Period Of Time Contains Part Of Another One (6 Or More Hours Of The Second One)

查看:93
本文介绍了如何检查第一段时间是否包含另一段时间(第二段时间为6小时或更长时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会在例子中提交我的问题:

我有一个司机在星期一下午3点到达,并在星期五凌晨0:30离开。

我有检查我必须支付多少晚上的费用。我在晚上9点到早上7点之间计算一晚的时间,并且必须至少6小时。我有解决这个问题,请帮帮我。非常感谢:

Hi, i will submit my problem at example:
Ive got a driver that arrive in monday at 3pm and leaves in friday at 0:30am.
I have to check how many nights rate i have to pay him. Im counting one night rate for time between 9pm and 7am and must be at least 6hours. I have problem with solution of this, Please help me. Thanks a lot :

        private void btnOblicz_Click(object sender, EventArgs e)
        {
            //counting night rates

            int totalRatesCount = 0;

            for(int i = 0; i < dgvDelegacje.Rows.Count - 1; i++)
            {
                DateTime departure = DateTime.ParseExact(dgvDelegacje.Rows[i +      
1].Cells[3].Value.ToString() +
" " + dgvDelegacje.Rows[i                                                               +1].Cells[4].Value.ToString() +
"-" + dtpOd.Value.Year, "HH:mm dd-MM-yyyy", plPL, DateTimeStyles.None);

                DateTime arrival =   DateTime.ParseExact(dgvDelegacje.Rows[i].Cells[6].Value.ToString() +
" " + dgvDelegacje.Rows[i].Cells[5].Value.ToString() + "-" + dtpOd.Value.Year, "HH:mm dd-MM-yyyy", plPL, DateTimeStyles.None);

                totalRatesCount += countNightRates(arrival, departure);
            }


        }

        private int countNightRates(DateTime _dateArrival, DateTime _dateDeparture)
        {
            //variables
            int ratesCount;

            DateTime nightHourFrom = DateTime.ParseExact(tbStawkaNocnaOD.Text, "HH:mm", CultureInfo.InvariantCulture);

            DateTime nightHourTo = DateTime.ParseExact(tbStawkaNocnaDo.Text, "HH:mm", CultureInfo.InvariantCulture);

            TimeSpan diffrence = _dateDeparture.Subtract(_dateArrival);

            //if diffrence is less than 6hours there is no rate
            if(diffrence.TotalHours < 6)
            {
                return 0;
            }
            else if (diffrence.TotalHours >= 6)
            {

            }


            return ratesCount;
        }



我试着写下一个条件,但它不能正常工作。 IN datagrid是用于到达和离开的小时和日期(dd / mm)的单元格。我仍然在努力,因为我发现了一些不好的东西。我只是想知道是否有任何简单的方法来比较这个日期而不是写所有可能性的条件


i tried to write next conditions but it doesnt work well. IN datagrid are cells for hour and date(dd/mm) for arrival and leave. im still working on it, as i figure something out ill post it. Im just wondering if there is any simply way to compare tthis dates instead of writting conditions for all posibilities

推荐答案

我知道给你一个完整的解决方案并不是一个好的方法练习,但我无法抗拒。假设这些值:

I know that giving you a complete solution isn't a good practice but I couldn't resist. Assuming these values:
var arrival = new DateTime(2015, 9, 7, 15, 0, 0);
var departure = new DateTime(2015, 9, 11, 0, 30, 0);
var nightHourFrom = TimeSpan.FromHours(21);
var nightHourTo = TimeSpan.FromHours(7);
var hoursRequired = TimeSpan.FromHours(6);



对于每一个,您都有一定的阈值才有资格获得报销。你必须在凌晨1点之前到达,否则你不能在凌晨3点之前离开。


For each nigh you have certain thresholds to be eligible for the reimbursement. You must arrive before 1am or you must not depart before 3am.

var arrivalThreshold = nightHourTo - hoursRequired;
var departureThreshold = TimeSpan.FromDays(-1) + nightHourFrom + hoursRequired;



然后你简单地迭代所有日子并检查条件是否适用:


Then you simply iterate trough all the days and check if the condition applies:

int count = 0;

for(var date = arrival.Date; date <= departure.Date; date = date.AddDays(1))
{
    if((arrival < date + arrivalThreshold) && (departure > date + departureThreshold))
    {
        count++;
    }
}



注意编码风格,创建分离代码逻辑单元的方法。尝试通过参数传递方法所需的所有数据。这样可以使您的代码清晰可读。


Be careful about your coding style, create methods to separate logical units of the code. Try to pass all data required by the methods via parameters. This will allow to keep your code clean a readable.


这篇关于如何检查第一段时间是否包含另一段时间(第二段时间为6小时或更长时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆