【问题标题】:question about date format - 16:15 to 16:00关于日期格式的问题 - 16:15 至 16:00
【发布时间】:2019-01-16 07:40:12
【问题描述】:

轮班是这样的:

1- 00:00 ->08:00
2- 08:00 ->16:00
3- 16:00 ->00:00

在轮班中,工人会做一些简短的工作。

有时工作从 15:55 开始,到 16:15 结束。

我必须分两班继续工作。

例如15:55 ->16:00 and 16:00 -> 16:15

我得到的值是 16:15 和它的日期。

例如'2019-01-15 16:17:20.123'

如何将'2019-01-15 16:17:20.123' 转换为'2019-01-15 16:00:00.000'

【问题讨论】:

  • 到目前为止您尝试过什么?你被困在哪里了?请提供一些代码来证明您的能力。
  • 3班开始时间有错别字吗?
  • 请包括minimal reproducible example 以表明您在当前状态下解决问题的努力 - 要求不费力地编写示例代码在这里被认为是题外话。
  • 欢迎来到stackoverflow。请花一分钟时间回答tour,尤其是如何How to Askedit 您的问题。
  • "我如何将 '2019-01-15 16:17:20.123' 转换为 '2019-01-15 16:00:00.000'" - DateTime result = new DateTime(sourceDate.Year, sourceDate.Month, sourceDate.Day, sourceDate.Hour, 0, 0);

标签: c# .net algorithm datetime timespan


【解决方案1】:

对于班次计算,您可以使用此代码

class ShiftInfo : IEquatable<ShiftInfo>
{
    public ShiftInfo(TimeSpan startTime, string name)
    {
        this.StartTime = startTime;
        this.Name = name;
    }

    public TimeSpan StartTime
    {
        get;
        private set;
    }

    public string Name
    {
        get;
        private set;
    }

    public bool Equals(ShiftInfo other)
    {
        return StartTime.Equals(other.StartTime);
    }

    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return false;
        }

        if (obj is ShiftInfo)
        {
            return this.Equals((ShiftInfo)obj);
        }

        return false;
    }

    public override int GetHashCode()
    {
        return StartTime.GetHashCode();
    }
}

class ShiftConfig : IEnumerable<ShiftInfo>
{
    private readonly ICollection<ShiftInfo> _shiftInfos;
    public ShiftConfig(params ShiftInfo[] shiftInfos)
    {
        _shiftInfos = shiftInfos.Distinct().OrderBy(e => e.StartTime).ToList();
    }

    public ShiftConfig(HashSet<ShiftInfo> shiftInfos)
    {
        _shiftInfos = shiftInfos.OrderBy(e => e.StartTime).ToList();
    }

    public IEnumerator<ShiftInfo> GetEnumerator()
    {
        return _shiftInfos.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _shiftInfos.GetEnumerator();
    }
}

class ShiftWorkItem
{
    public ShiftWorkItem(ShiftInfo shift, DateTime shiftFrom, DateTime shiftUntil, DateTime workFrom, DateTime workUntil)
    {
        Shift = shift;
        ShiftFrom = shiftFrom;
        ShiftUntil = shiftUntil;
        WorkFrom = workFrom;
        WorkUntil = workUntil;
    }

    public ShiftInfo Shift
    {
        get;
        private set;
    }

    public DateTime ShiftFrom
    {
        get;
        private set;
    }

    public DateTime ShiftUntil
    {
        get;
        private set;
    }

    public TimeSpan ShiftDuration
    {
        get
        {
            return ShiftUntil - ShiftFrom;
        }
    }

    public DateTime WorkFrom
    {
        get;
        private set;
    }

    public DateTime WorkUntil
    {
        get;
        private set;
    }

    public TimeSpan WorkDuration
    {
        get
        {
            return WorkUntil - WorkFrom;
        }
    }
}

static class ShiftConfigExtensions
{
    public static IEnumerable<ShiftWorkItem> EnumerateShifts(this ShiftConfig config, DateTime from, TimeSpan duration)
    {
        return EnumerateShifts(config, from, from.Add(duration));
    }

    public static IEnumerable<ShiftWorkItem> EnumerateShifts(this ShiftConfig config, DateTime from, DateTime until)
    {
        DateTime day = from.Date.AddDays(-1);
        DateTime? shiftStart = null;
        ShiftInfo lastShift = null;
        while (true)
        {
            foreach (var shift in config)
            {
                var shiftEnd = day.Add(shift.StartTime);
                if (shiftStart != null)
                {
                    if ((shiftStart.Value <= from && shiftEnd >= from) || (shiftStart.Value <= until && shiftEnd >= until) || (shiftStart.Value > from && shiftEnd <= until))
                    {
                        var workFrom = shiftStart.Value < from ? from : shiftStart.Value;
                        var workUntil = shiftEnd > until ? until : shiftEnd;
                        yield return new ShiftWorkItem(lastShift, shiftStart.Value, shiftEnd, workFrom, workUntil);
                    }
                }

                if (shiftEnd >= until)
                {
                    yield break;
                }

                shiftStart = shiftEnd;
                lastShift = shift;
            }

            day = day.AddDays(1);
        }
    }
}

用法

public static void Main(string[] args)
{
    var sc = new ShiftConfig(
        new ShiftInfo(TimeSpan.FromHours(6), "early"), 
        new ShiftInfo(TimeSpan.FromHours(14), "late"), 
        new ShiftInfo(TimeSpan.FromHours(22), "night"));
    Console.WriteLine("                      |          SHIFT          |          WORK           ");
    Console.WriteLine("   Date    Shiftname  |   from    until   dur.  |   from    until   dur.  ");
    Console.WriteLine("======================|=========================|=========================");
    foreach (var item in sc.EnumerateShifts(new DateTime(2019, 01, 01, 02, 37, 25), TimeSpan.FromHours(28.34)))
    {
        Console.WriteLine("{0:yyyy-MM-dd} {1,-10} | {2:HH:mm:ss} {3:HH:mm:ss} {4:0.00}h | {5:HH:mm:ss} {6:HH:mm:ss} {7:0.00}h", item.ShiftFrom.Date, item.Shift.Name, item.ShiftFrom, item.ShiftUntil, item.ShiftDuration.TotalHours, item.WorkFrom, item.WorkUntil, item.WorkDuration.TotalHours);
    }
}

生成

|移位 |工作 日期班次名称 |从直到杜尔。 |从直到杜尔。 =======================|==========================|= ========================= 2018-12-31晚 | 22:00:00 06:00:00 8.00h | 02:37:25 06:00:00 3.38 小时 2019-01-01 早 | 06:00:00 14:00:00 8.00 小时 | 06:00:00 14:00:00 8.00h 2019-01-01 迟到 | 14:00:00 22:00:00 8.00 小时 | 14:00:00 22:00:00 8.00h 2019-01-01晚 | 22:00:00 06:00:00 8.00h | 22:00:00 06:00:00 8.00h 2019-01-02 早 | 06:00:00 14:00:00 8.00 小时 | 06:00:00 06:57:49 0.96h

.net fiddle sample

或覆盖您的样本

public static void Main(string[] args)
{
    var sc = new ShiftConfig(
        new ShiftInfo(TimeSpan.FromHours(0), "night"), 
        new ShiftInfo(TimeSpan.FromHours(8), "early"), 
        new ShiftInfo(TimeSpan.FromHours(16), "late"));
    Console.WriteLine("                      |          SHIFT          |          WORK           ");
    Console.WriteLine("   Date    Shiftname  |   from    until   dur.  |   from    until   dur.  ");
    Console.WriteLine("======================|=========================|=========================");
    foreach (var item in sc.EnumerateShifts(new DateTime(2019, 01, 01, 15, 55, 00), TimeSpan.FromMinutes(20)))
    {
        Console.WriteLine("{0:yyyy-MM-dd} {1,-10} | {2:HH:mm:ss} {3:HH:mm:ss} {4:0.00}h | {5:HH:mm:ss} {6:HH:mm:ss} {7:0.00}h", item.ShiftFrom.Date, item.Shift.Name, item.ShiftFrom, item.ShiftUntil, item.ShiftDuration.TotalHours, item.WorkFrom, item.WorkUntil, item.WorkDuration.TotalHours);
    }
}

我们得到

|移位 |工作 日期班次名称 |从直到杜尔。 |从直到杜尔。 =======================|==========================|= ========================= 2019-01-01 早 | 08:00:00 16:00:00 8.00h | 15:55:00 16:00:00 0.08h 2019-01-01 迟到 | 16:00:00 00:00:00 8.00 小时 | 16:00:00 16:15:00 0.25h

.net fiddle sample

【讨论】:

    【解决方案2】:

    您可以创建一个舍入函数以四舍五入到最接近的小时等。并使用 SeM 的建议,通常在这里询问时,您会显示您已经尝试过的源代码。

    public static DateTime RoundDate(DateTime input) 
    {
        var hour = input.Minutes > 30 ? input.Hour + 1 : input.Hour; //Rounding to get the nearest hour
        return new DateTime(input.Year, input.Month, input.Day, hour, 0);
    }
    

    【讨论】:

      【解决方案3】:

      您可以指定阈值并编写根据它评估和设置日期时间的函数

      var threshold = 15; //minutes
      
      var dt = DateTime.Now.AddMinutes(-20);
      //var dt = DateTime.Now;
      DateTime resultDt;
      
      DateTime thUp = dt.AddMinutes(15);
      DateTime thDown = dt.AddMinutes(-15);
      
      if (thUp.Hour != dt.Hour)
          resultDt = new DateTime(dt.Year, dt.Month, dt.Day, thUp.Hour, 0, 0);
      else if (thDown.Hour != dt.Hour)
          resultDt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0);
      else 
          resultDt = dt;
      

      Fiddle

      【讨论】:

        猜你喜欢
        • 2021-07-10
        • 2012-04-12
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 2021-11-09
        • 1970-01-01
        相关资源
        最近更新 更多