C# - TimeSpan 不适用于超过一天的秒数

标签 c# timespan

我觉得这个问题的标题不好,但我找不到更好的了。

我想做的是让用户能够设置倒计时计时器。 (看图一): enter image description here

应用程序接受用户输入并检查用户选择的时间单位,然后将其转换为秒并将值分配给 int 变量。

private int seconds = -1;

private void enable_button_Click(object sender, EventArgs e)
{
    int amount = Convert.ToInt32(time_numericUpDown.Value);
    string unit = tUnits_comboBox.Text;

    // Check what time is chosen then convert it to seconds
    if (unit == "Seconds")
        seconds = amount;
    else if (unit == "Minutes")
        seconds = amount * 60;
    else if (unit == "Hours")
        seconds = amount * 3600;
    else if (unit == "Days")
        seconds = amount * 86400;

    // Clock it!
    timer.Enabled = true;
}

然后,计时器应该以人类可读的格式显示时间,我为此使用了这段代码:

private void timer_Tick(object sender, EventArgs e)
{
    // Verify if the time didn't pass
    if (seconds == 0)
    {
        // If the time is over, do the specified action
        timer.Enabled = false;
        operation(); // << This is the function that does the Action!
    }
    else
    {
        // Continue counting
        seconds -= 1;

        TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);

        string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
                        timeSpan.Hours,
                        timeSpan.Minutes,
                        timeSpan.Seconds);

        status_label.Text = "Restarting in " + answer;
    }
}

当“seconds”变量的值代表一天或更短时间时,这非常有效,但当它超过 24 小时时,它只显示 24 小时的状态。我做错了什么?

(问题):

enter image description here

最佳答案

所有不以 Total 开头的属性仅包含不适合下一个更高属性的剩余部分。

换句话说:Days 属性将包含值 1

关于C# - TimeSpan 不适用于超过一天的秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18699854/

相关文章:

c# - 如何测试头顶的WebSocket?

PowerShell New-TimeSpan 友好地显示为天(s)小时(s)分钟(s)秒(s)

c# - 32 位和 64 位 Windows 之间的日期时间粒度

c# - 如何将 TimeSpan 序列化为 XML

c# - 时间跨度数据类型 C#

c# - 将文件扩展名关联到 C# 应用程序中的应用程序

c# - 无法在 WCF 中转换为相同类型

C# : Manually creating a console. 写行

c# - 获取实现特定开放通用类型的所有类型

c# - 查找当前时间是否落在多个时间范围之间