如何将十进制数转换为时间,反之亦然

时间:2009-08-28 07:53:29

标签: c#

这是一个例子

            if 8.30 is there it should be 8 hours 30 minute
            if 8 hour 20 minutes  then 8.20  

           Please tell whether it is possible ? if yes
           how ?      

6 个答案:

答案 0 :(得分:11)

当人们谈论小时时,他们通常意味着0.1 = 6分钟。

因此,转换8.3的正确公式是:

8小时+ 3 * 6分钟= 8:18

要将8:20转换为十进制,它将是:

8 + 20/6 = 8.333333(可能是8.3)

答案 1 :(得分:3)

如果它始终用分隔,并且您希望它用于显示,那么只需使用它:

var ar="8.30".split(new[]{'.'});

Console.Write("{0} hours {1} minutes",ar[0], ar[1]);

PS:我们肯定在数组中有两个元素,但请在使用ar之前检查数组ar[1]的长度

答案 2 :(得分:2)

我的方法看起来像这样。 (这是红宝石所以你必须自己转换它,但逻辑在这里很重要)

  def zeropad(number)
    return ((number.to_f < 10) ? "0" : "") + number.round.to_s
  end

  def decimal_to_time(value)
    t = value.split(".") #returns an array of ["hour", "minutes"]
    hours, minutes = t[0], t[1]
    minutes = zeropad( (minutes.to_f / 10**minutes.length) * 60 ) # parse the minutes into a time value
    return (minutes.to_i == 0) ? hours : hours + ":" + minutes
  end

  def findTime(value)
    value =~ /^\d+\.\d+/ ? decimal_to_time(value) : value
  end

其中findTime(“5.015”)为您提供适当的时间值。

我已经在以下测试中对此进行了测试,它们都通过了。

     | entered_time   | expected_results|
      | "5.6"         | "5:36"          |
      | "5.9"         | "5:54"          |
      | "5.09"        | "5:05"          |
      | "5.0"         | "5"          |
      | "5.00"        | "5"          |
      | "5.015"       | "5:01"          |
      | "6.03"        | "6:02"          |
      | "5.30"        | "5:18"          |
      | "4.2"         | "4:12"        |
      | "8.3"     | "8:18"           |
      | "8.33"    | "8:20"            |
      | "105.5"       | "105:30"        |
      | "16.7"        | "16:42"         |
      | "Abc"         | "Abc"           |
      | "5:36"    | "5:36"              | 
      | "5:44"    | "5:44"              |   

答案 3 :(得分:1)

这里有几个扩展方法(用于DateTime和Decimal):

public static class DecimalToTimeConverters
{
    public static DateTime ToDateTime(this decimal value)
    {
        string[] parts = value.ToString().Split(new char[] { '.' });

        int hours = Convert.ToInt32(parts[0]);
        int minutes = Convert.ToInt32(parts[1]);

        if ((hours > 23) || (hours < 0))
        {
            throw new ArgumentOutOfRangeException("value", "decimal value must be no greater than 23.59 and no less than 0");
        }
        if ((minutes > 59) || (minutes < 0))
        {
            throw new ArgumentOutOfRangeException("value", "decimal value must be no greater than 23.59 and no less than 0");
        }
        DateTime d = new DateTime(1, 1, 1, hours, minutes, 0);
        return d;
    }

    public static Decimal ToDecimal(this DateTime datetime)
    {
        Decimal d = new decimal();
        d = datetime.Hour;
        d = d + Convert.ToDecimal((datetime.Minute * 0.01));

        return d;
    }
}

我在一个新的空白页面中使用以下内容在ASP.net网页(我当时打开了一个Web项目)中非常快速地测试了这一点,它看起来很有效:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Decimal d = new decimal();
    d = 3.45M;
    Response.Write(d.ToDateTime().ToString());
    Response.Write("<br />");
    DateTime d2 = new DateTime(2009, 1, 1, 4, 55, 0);
    Response.Write(d2.ToDecimal().ToString());
}

答案 4 :(得分:0)

按照Rob但替换

string[] parts = value.ToString().Split(new char[] { '.' }); 
int hours = Convert.ToInt32(parts[0]); 
int minutes = Convert.ToInt32(parts[1]); 

as

int hours = (int)value; 
int minutes = (int)((value - minutes) * 100); 

没有字符串或依赖当前文化(假设'。'是小数点)

答案 5 :(得分:0)

如何将txtDuration.Text值解析为十进制值?

if (txtDuration.Text)
{
    var duration = int.Parse(txtDuration.Text);
    var timespan = Boolean.Parse(hdfToggleDuration.Value) ? new TimeSpan (0, 0, duration, 0) : new TimeSpan (0, duration, 0, 0);
    DateTime end = start.Add(timespan);
}