本帖最后由 lxcnn 于 2010-04-18 07:19:56 编辑

解决方案 »

  1.   

    你只说了结束条件,begintime呢?
      

  2.   

    /*
     * Created by SharpDevelop.
     * User: ChangLiang Li
     * Date: 2010-4-14
     * Time: 21:08
     * 
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */
    using System;namespace CSharpConsole
    {
    class Program
    {
    public static void Main(string[] args)
    {

    // TODO: Implement Functionality Here
    Console.WriteLine(GetTT(new DateTime(2002,1,1,8,0,0),new DateTime(2002,1,1,9,0,0)));
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    public static double GetTT(DateTime  begintime, DateTime endtime)
    {
    double time=0.0;
    TimeSpan ts1 = new TimeSpan(begintime.Ticks);
                TimeSpan ts2 = new TimeSpan(endtime.Ticks);
                time+=ts2.Subtract(ts1).Days==0?1:ts2.Subtract(ts1).Days;
                int hour=endtime.Hour;
                if(hour<14) return time;
                 else
                 if(hour<18) return time+0.5;
                   else return time+1.0;
    } }
    }
    还有什么问题?
      

  3.   

    TimeSpan objTimeSpan=现在时间-开始时间
    得到objTimeSpan的总小时数
    然后用这个总小时数 去除24  留下余数  
    加上条件判断
      

  4.   

    感谢 LCL_data 你的代码我现在试试,好用联系你~~~~
      

  5.   

    /*
     * Created by SharpDevelop.
     * User: ChangLiang Li
     * Date: 2010-4-14
     * Time: 21:08
     * 
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */
    using System;namespace CSharpConsole
    {
    class Program
    {
    public static void Main(string[] args)
    {

    // TODO: Implement Functionality Here
    Console.WriteLine(GetTT(new DateTime(2002,1,1,8,0,0),new DateTime(2002,1,1,13,0,0)));
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    public static double GetTT(DateTime  begintime, DateTime endtime)
    {
    double time=0.0;
    TimeSpan ts1 = new TimeSpan(begintime.Ticks);
                TimeSpan ts2 = new TimeSpan(endtime.Ticks);
                time+=ts2.Subtract(ts1).Days;
                if(time<1.0) return 1.0; //不到一天
                else{
                int hour=endtime.Hour;
                if(hour<14) return time;
                 else
                 if(hour<18) return time+0.5;
                   else return time+1.0;
                }
    } }
    }
      

  6.   

    To LCL_data你好,你的代码我试了下,有问题  如 begintime 是 "2010/4/14 22:00:00"
    如 endtime 是 "2010/4/15 15:00:00"仍然返回 1.0应该返回的是 1.5
      

  7.   

    /*
     * Created by SharpDevelop.
     * User: ChangLiang Li
     * Date: 2010-4-14
     * Time: 21:08
     * 
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */
    using System;namespace CSharpConsole
    {
    class Program
    {
    public static void Main(string[] args)
    {

    // TODO: Implement Functionality Here
    Console.WriteLine(GetTT(new DateTime(2002,1,1,22,0,0),new DateTime(2002,1,2,12,0,0)));
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    public static double GetTT(DateTime  begintime, DateTime endtime)
    {
    double time=0.0;
    time=endtime.Day-begintime.Day;           
                if(time<1.0) return 1.0; //不到一天
                else{
                int hour=endtime.Hour;
                if(hour<14) return time;
                 else
                 if(hour<18) return time+0.5;
                   else return time+1.0;
                }
    } }
    }
      

  8.   

    To LCL_data感谢你写了这么多,但仍然有问题
     
    如 begintime 是 "2010/4/14 22:00:00"
    如 endtime 是 "2010/4/15 12:00:00"返回 0 了应该返回的是 1.0
      

  9.   

    睡前最后一帖...当天按1天处理...
    public double GetTT(DateTime begintime, DateTime endtime)
    {
        if (begintime >= endtime)
            throw new ArgumentOutOfRangeException("begintime");    double result = 1;    //租住天数...不要时间,无用...
        int days = (endtime.Date - begintime.Date).Days;    //当天退房
        if (days < 1)
            return result;    //退房当天租住小时数...其他时间数据都无用...
        int hours = (endtime - endtime.Date).Hours;    switch (hours)
        {
            //14:00-18:00退房
            case 14:
            case 15:
            case 16:
            case 17:
                result = days + 0.5;
                break;
            //18:00后退房
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
                result = days + 1;
                break;
            //14:00前退房
            default:
                result = days;
                break;
        }    return result;
    }
      

  10.   

    完全同意vrhero老兄的算法
    其实有些看上去很复杂的计算,如果把逻辑关系理顺了,计算方法非常简单
    附加一句:vrhero是个有爱心的人,楼主应该按照他说的做
      

  11.   

    恩,我9楼的算法就是这个意思
    你的switch是不是有点冗余?我的if代码少很多啊,呵呵
      

  12.   


    没那么麻烦,看看这个:        static double ppp1(DateTime begin,DateTime end)
            {
                double d = end.Day - begin.Day;
                if (d < 1) d = 1;
                int h = end.Hour;
                if (h < 14) return d;
                if (h < 18) return d + 0.5;
                return d + 1;
            }
      

  13.   

    代码只是个例子,用switch相当清晰而且非常容易扩展...ps:26楼的代码根本就不合格,不同月份必然出错,会被PM骂死...
      

  14.   

    这个开源编译器不支持格式化代码
    。。月份考虑了
    /*
     * Created by SharpDevelop.
     * User: ChangLiang Li
     * Date: 2010-4-14
     * Time: 21:08
     * 
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */
    using System;namespace CSharpConsole
    {
        class Program
        {
            public static void Main(string[] args)
            {
                
                // TODO: Implement Functionality Here
                Console.WriteLine(GetTT(new DateTime(2002,1,1,13,0,0),new DateTime(2002,2,1,12,0,0)));
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
            public static double GetTT(DateTime  begintime, DateTime endtime)
            {
                double time=0.0;           
                time= begintime.Subtract(endtime).Days;
                if(time<0) time=-time;
                if(endtime.Hour<begintime.Hour) time++;
                if(time<1.0) return 1.0; //不到一天
                else{
                int hour=endtime.Hour;
                switch (hour)
                  {
             //14:00-18:00退房
             case 14:
             case 15:
             case 16:
             case 17:
                return  time + 0.5;           
             //18:00后退房
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            case 23:
                return time + 1;            
            //14:00前退房
            default:
                return time;
               
        }
                }
            }    }
    }
      

  15.   


    static double GetDays(DateTime startTime, DateTime endTime)
            {
                DateTime startDate = startTime.Date;
                if (startTime.Hour < 4) //凌晨4点前入住算前一天
                    startDate = startDate.AddDays(-1);
                DateTime endDate = endTime.Date;
                double days = (endDate - startDate).Days;
                if (endTime.Hour >= 18) //18点后退房
                {
                    days = days + 1;
                }
                else if (endTime.Hour >= 14)  //14点后退房
                {
                    days = days + 0.5;
                }
                return days;
            }
      

  16.   

    错了,少算当天上午入住,当天上午退的。
    static double GetDays(DateTime startTime, DateTime endTime)
            {
                DateTime startDate = startTime.Date;
                if (startTime.Hour < 4) //凌晨4点前入住算前一天
                    startDate = startDate.AddDays(-1);
                DateTime endDate = endTime.Date;
                double days = (endDate - startDate).Days;
                if (endTime.Hour >= 18) //18点后退房
                {
                    days = days + 1;
                }
                else if (endTime.Hour >= 14)  //14点后退房
                {
                    days = days + 0.5;
                }
                if (days < 0.1)
                    days = 1;
                return days;
            }
      

  17.   


    楼主,给分吧:
            static double ppp1(DateTime begin,DateTime end)
            {
                double d = (end.Date - begin.Date).Days;
                if (d < 1) d = 1;
                int h = end.Hour;
                if (h < 14) return d;
                if (h < 18) return d + 0.5;
                return d + 1;
            }