[ACCEPTED]-How to Convert decimal number to time or vice versa-c#

Accepted answer
Score: 11

When people talk about decimal hours, they 5 usually mean 0.1 = 6 minutes.

So, the correct 4 formula to convert 8.3 would be:

8 hours 3 + 3 * 6 minutes = 8:18

To convert 8:20 to 2 decimal it would be:

8 + 20/6 = 8.333333 1 (probably round to 8.3)

Score: 3

If it always be separated with . and you 4 want it for displaying then simply use this:

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

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

PS: Here 3 we are sure to have two elements in array, but 2 please check length of array ar before using 1 ar[1]

Score: 2

My approach would look something like this. (This 5 is ruby so you'll have to convert it yourself 4 but the logic is whats important here)

  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

Where 3 findTime("5.015") gives you the appropriate 2 time value.

I've tested this across the following 1 tests and they all pass.

     | 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"              |   
Score: 1

Here's a couple of extension methods (for 5 DateTime and Decimal) that do the job:

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;
    }
}

I 4 tested this very quickly in an ASP.net webpage 3 (I had a web project open at the time) using 2 the following in a new blank page, and it 1 seemed to work a treat:

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());
}
Score: 0

As per Rob but substitute

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); 

no strings 2 or reliance on current culture (the assumption 1 that the '.' is the decimal point)

Score: 0

How can I parse the txtDuration.Text Value into a decimal 1 value?

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);
}

More Related questions