以百分比表示,如何获得两个日期时间之间的时间?

时间:2019-04-04 22:25:44

标签: c# datetime

我认为描述此问题的最佳方法是使用示例。范围是从4:00 am到4:00 pm。设为0%时,我想找回4:00am。对于100%,我想找回4:00pm。 50%时,我想找回10:00am

问题是,如何通过此签名获得此功能?

string GetTime(percentage, startTime, endTime)

4 个答案:

答案 0 :(得分:4)

假设两个日期时间都在同一时区,并且没有转换(DST开始/结束,本地时间已更改,等等):

public static void Main()
{
    var startTime = new DateTime(2019, 01, 01, 4, 0, 0);
    var endTime = new DateTime(2019, 01, 01, 16, 0 , 0);

    // prints 10:00 AM
    Console.WriteLine(GetTime(0.5, startTime, endTime));
}

private static string GetTime(double percentage, DateTime startTime, DateTime endTime)
{
    // get the difference between the dates
    // you could use TotalSeconds or a higher precision if needed
    var diff = (endTime - startTime).TotalMinutes;

    // multiply the result by the percentage
    // assuming a range of [0.0, 1.0]
    double minutes = diff * percentage;

    // add the minutes (or precision chosen) to the startTime
    var result = startTime.AddMinutes(minutes);

    // and get the result
    return result.ToShortTimeString();
}

您可以在这个小提琴中进行测试:https://dotnetfiddle.net/g4gHLJ

答案 1 :(得分:2)

您应该返回TimeSpan而不是string。这是我的处理方式:

static TimeSpan GetTime(double percentage, TimeSpan startTime, TimeSpan endTime)
{
    var percentageInTicks = (long)((endTime - startTime).Ticks * percentage);
    return startTime.Add(TimeSpan.FromTicks(percentageInTicks));
}

用法:

TimeSpan startTime = new TimeSpan(4, 0, 0);
TimeSpan endTime = new TimeSpan(16, 0, 0);
double[] percentages = new[] { 0, 0.5, 1 };

foreach (double percentage in percentages)
{
    var result = GetTime(percentage, startTime, endTime);
    Console.WriteLine(result.ToString());
}

输出:

04:00:00
10:00:00
16:00:00

编辑:如果愿意,您可以使用DateTime做基本相同的事情:

static TimeSpan GetTime(double percentage, DateTime startDate, DateTime endDate)
{
    var percentageInTicks = (long)((endDate - startDate).Ticks * percentage);
    return startDate.TimeOfDay.Add(TimeSpan.FromTicks(percentageInTicks));
}

用法:

DateTime startDate = DateTime.Today.Add(new TimeSpan(4, 0, 0));
DateTime endDate = DateTime.Today.Add(new TimeSpan(16, 0, 0));
double[] percentages = new[] { 0, 0.5, 1 };

foreach (double percentage in percentages)
{
    var result = GetTime(percentage, startDate, startDate);
    Console.WriteLine(result.ToString());
}

答案 2 :(得分:0)

仅使用DateTimes作为输入和输出的方法如下:

 public static DateTime GetTime(double percentage, DateTime startTime, DateTime endTime)
 {
     TimeSpan diff = endTime - startTime;
     long percentOfTimeAsTicks = (long) (percentage * diff.Ticks);
     return startTime + new TimeSpan(percentOfTimeAsTicks);
 }

正如@MattJohnson所指出的,两个DateTime必须基于相同的基础(两个UTC或两个都在同一时区中具有相同的夏/冬状态)。

使用Ticks而不是秒或分钟可以使您获得最高的精度。

这是一个简单的测试:

  var start = DateTime.Today;
  var end = DateTime.Now;
  Debug.WriteLine(Struct128.GetTime(0.5, start, end));

打印出的时间是上午9:00之前(目前这里是下午6:00之前)。

答案 3 :(得分:0)

首先,您需要确定开始时间和结束时间之间的时间长度,并计算需要添加到开始时间中的那一部分...

DateTime GetTime(double percentage, DateTime startTime, DateTime endTime)
{
    var duration = endTime - startTime;
    var partDuration = percentage * (double)duration.TotalMilliseconds;
    return startTime.AddMilliseconds(partDuration);
}


GetTime(0.5, DateTime.Today.AddHours(4), DateTime.Today.AddHours(16)).Dump();