如何将int / double格式化为可读时间值

时间:2018-01-09 23:02:33

标签: c#

我有一个程序,建议一天的时间离开去按时狩猎鸭/鹅。它询问用户日出时间,驱动器需要多长时间等,并计算您应该准时离开的时间。我的程序有效但我需要格式化结果。例如:7的日出,30的驱动,30的设置,30的步行,等待30的结果为4.5(这是正确的)但我希望它读为4:30。有什么建议??拜托,谢谢!

Console.WriteLine("Enter sunrise");//7AM would be 420minutes
double sunrise = Convert.ToInt32(Console.ReadLine());
double minutes = sunrise * 60;

double legal = 30;//allowed to shoot 30min before sunrise

Console.WriteLine("How many minutes will it take to drive to" + 
"your destination: ");
double drive = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("How many minutes will it take to set up: ");
double setup = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("How many minutes will it take to walk from " + 
"your vehicle to the actual hunting spot:");
double walking = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Amount of time between setting up and " + 
"pulling the trigger:");
double waiting = Convert.ToInt32(Console.ReadLine());

double departure = ((minutes - (legal + drive + setup + walking 
+ waiting))/60);//6.5
Console.WriteLine("If sunrise is at " + sunrise +"AM, and it 
takes " + drive + " minutes to drive there you should leave at" + 
departure + "AM");

6 个答案:

答案 0 :(得分:1)

更改您的

double ... = Convert.ToInt32(Console.ReadLine());

TimeSpan ... = TimeSpan.FromMinutes(int.Parse(Console.ReadLine()); 

然后使用:

TimeSpan departure = sunrise - (legal + drive + setup + waiting);

Console.WriteLine("If sunrise is at {0}AM, and it takes {1} minutes to drive there you should leave at {2}AM", 
     sunrise, drive.Minutes, departure);

然后,您可以使用TimeSpan formatting进行播放,直至获得所需的输出。

答案 1 :(得分:0)

DateTime.Today.AddHours(departure).ToLongTimeString()

答案 2 :(得分:0)

一种方法是将日出时间作为用户的时间格式(hh:mm),其他值作为分钟。然后,您可以将日出转换为DateTime,减去30分钟,然后减去其余时间的总和。这将为您留下DateTime结果,该结果很容易使用AM / PM信息进行格式化。

为了帮助从用户那里获得有效的输入,我将创建以下两种方法,从用户那里获得TimeSpanint(如果他们输入不正确的数据,还需要重试) 。请注意,获得TimeSpan的只需要小时和分钟,并自动为秒添加:00

private static TimeSpan GetTimeFromUser(string prompt)
{
    Console.Write(prompt);
    TimeSpan result;

    while (!TimeSpan.TryParse(Console.ReadLine() + ":00", out result))
    {
        Console.WriteLine("Please use format hh:mm for the time.");
        Console.Write(prompt);
    }

    return result;
}

private static int GetIntFromUser(string prompt)
{
    Console.Write(prompt);
    int result;

    while (!int.TryParse(Console.ReadLine(), out result))
    {
        Console.WriteLine("Please enter a valid integer.");
        Console.Write(prompt);
    }

    return result;
}

有了这些辅助功能,我们可以非常轻松地从用户那里获取信息,然后显示结果:

private static void Main()
{
    DateTime sunrise = DateTime.Today.Add(GetTimeFromUser("Enter sunrise time: "));
    DateTime legalStartTime = sunrise.AddMinutes(-30);

    int driveTime = GetIntFromUser(
        "How many minutes will it take to drive to your destination: ");
    int setupTime = GetIntFromUser(
        "How many minutes will it take to set up: ");
    int walkTime = GetIntFromUser(
        "How many minutes will it take to walk from your vehicle to the hunting spot: ");
    int waitTime = GetIntFromUser(
        "Number of minutes between setting up and pulling the trigger: ");
    int totalTime = driveTime + setupTime + walkTime + waitTime;

    DateTime departureTime = legalStartTime.AddMinutes(-totalTime);

    Console.WriteLine($"If sunrise is at {sunrise:hh:mm tt}, and it takes {totalTime} " +
        $"minutes to drive and get ready, you should leave at: {departureTime:hh:mm tt}");

    Console.WriteLine("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

<强>输出

enter image description here

答案 3 :(得分:0)

虽然引入的解决方案很少,但我觉得需要添加更完整但更天真的解决方案,它将sunrise作为 DateTime 处理,并将结果表示为时间。

Console.WriteLine("Sunrise at:");
var sunrise = DateTime.Parse(Console.ReadLine());

Console.WriteLine("How many minutes will it take to drive to your destination: ");
var drive = TimeSpan.FromMinutes(int.Parse(Console.ReadLine()));

Console.WriteLine("How many minutes will it take to set up: ");
var setup = TimeSpan.FromMinutes(int.Parse(Console.ReadLine()));

Console.WriteLine("How many minutes will it take to walk from your vehicle to the actual hunting spot:");
var walking = TimeSpan.FromMinutes(int.Parse(Console.ReadLine()));

Console.WriteLine("Amount of time between setting up and pulling the trigger:");
var waiting = TimeSpan.FromMinutes(int.Parse(Console.ReadLine()));

var legalTimeBeforeSunriseInMin = TimeSpan.FromMinutes(30);
var calculatedDelta = (legalTimeBeforeSunriseInMin + drive + setup + walking + waiting);
var departure = sunrise.Add(-calculatedDelta).ToLongTimeString();
Console.WriteLine($"If sunrise is at {sunrise.ToLongTimeString()}, and it takes {drive} minutes to drive there you should leave at {departure}");

答案 4 :(得分:0)

double t = 4.5;
TimeSpan s = TimeSpan.FromMinutes(t * 60);
string result = s.ToString(@"h\:mm");

答案 5 :(得分:-1)

如果你有像小时一样的时间

var departTimeInHours = 4.5;

并且您希望将其显示为带有冒号和AM / PM的12小时时间格式化字符串,一种可能性是从小数小时创建DateTime并使用该类型的内置字符串转换,具有本地化的优势(是否有非AM / PM本地化时间格式?)。将小数小时转换为DateTime的最简单方法是将它们转换为刻度(100纳秒单位),然后调用构造函数。

var strDepartTime = (new DateTime((long)(departTimeInHours*60*60*10000000)).ToString("t");