【问题标题】:C# method to retrieve a Work time slots based on few parameters基于几个参数检索工作时隙的 C# 方法
【发布时间】:2011-09-09 12:30:27
【问题描述】:

有人可以帮我用 c# 编写一个可以接受以下参数并提供工作时间列表的方法。

1.String workStartingTime

2.String workStopTime

3.int iBreaktime

4.int iWorkInterval

我确实编写了一个有效的代码,但该死的代码太难以维护和做作。提供了使用以下参数执行的示例输出:

workStartTime = "9:00 AM",

workStopTime = "6:00 PM",

ibreakTime = 10,

iWorkInterval = 60

输出:

9:00 AM - 10:00 AM

10:10 AM - 11:10 AM

11:20 AM - 12:20 PM

12:30 PM - 01:30 PM

希望您明白这一点,因为每个间隔都要留出一个间隙并跨越整个事情,直到工作结束时间。

注意:

顺便说一下,工作开始时间和结束时间像9:30 AM - 10:00 PM9:00 - 16:00 一样存储在数据库中,即12 小时或24 小时格式。

【问题讨论】:

    标签: c# asp.net datetime methods scheduling


    【解决方案1】:

    您可以使用免费Time Period Library for .NETCalendarPeriodCollector

    该工具支持各种过滤器,包括工作时间:

    // ----------------------------------------------------------------------
    public void CalendarPeriodCollectorSample()
    {
      CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
      filter.Months.Add( YearMonth.January ); // only Januaries
      filter.WeekDays.Add( DayOfWeek.Friday ); // only Fridays
      filter.CollectingHours.Add( new HourRange( 8, 18 ) ); // working hours
    
      CalendarTimeRange testPeriod =
        new CalendarTimeRange( new DateTime( 2010, 1, 1 ), new DateTime( 2011, 12, 31 ) );
      Console.WriteLine( "Calendar period collector of period: " + testPeriod );
      // > Calendar period collector of period:
      //            01.01.2010 00:00:00 - 30.12.2011 23:59:59 | 728.23:59
    
      CalendarPeriodCollector collector =
              new CalendarPeriodCollector( filter, testPeriod );
      collector.CollectHours();
      foreach ( ITimePeriod period in collector.Periods )
      {
        Console.WriteLine( "Period: " + period );
      }
      // > Period: 01.01.2010; 08:00 - 17:59 | 0.09:59
      // > Period: 08.01.2010; 08:00 - 17:59 | 0.09:59
      // > Period: 15.01.2010; 08:00 - 17:59 | 0.09:59
      // > Period: 22.01.2010; 08:00 - 17:59 | 0.09:59
      // > Period: 29.01.2010; 08:00 - 17:59 | 0.09:59
      // > Period: 07.01.2011; 08:00 - 17:59 | 0.09:59
      // > Period: 14.01.2011; 08:00 - 17:59 | 0.09:59
      // > Period: 21.01.2011; 08:00 - 17:59 | 0.09:59
      // > Period: 28.01.2011; 08:00 - 17:59 | 0.09:59
    } // CalendarPeriodCollectorSample
    

    【讨论】:

      【解决方案2】:
      void CreateTimes(DateTime start, DateTime end, int interval, TimeSpan work)
      {
          DateTime currentStart = start;
          while(currentStart < end && currentStart.Add(work)<end)
          {
              Console.WriteLine(String.Format("{0} - {1}", currentStart, currentStart.Add(work)));
              currentStart.AddMinutes(interval);
          }
      }
      

      【讨论】:

      • 您如何将9:30 AM - 6:00Pm 字符串转换为单独的日期时间实例,您是否还可以添加该代码 TimeSpan 您能否详细说明参数如何转换为您的方法参数
      • 您可以使用 DateTime.Parse 方法:msdn.microsoft.com/en-us/library/1k1skd40.aspx
      • 我想DateTime.Parse("(9:00 AM - 6:00 PM") 会产生一个期望无效的字符串
      • 好吧,使用 "9:00 AM - 6:00 PM".Split("-")[0].Trim() 和 "9:00 AM - 6:00 PM".Split ("-")[1].Trim()
      • @Deeptechtons - DateTime.Parse("9:30 AM") 在我的机器上工作.. 因为它包含 AM 您可能需要指定 CultureInfo:DateTime.Parse("9:30AM", CultureInfo.GetCultureInfo("en-US"))
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多