asp.netdecimalmaskededitextender

Convert time to decimals in .net


Is there an easy way to present time (hh:mm) as a decimal value? Example, 01:08 should become 1,13.

I have an asp:textbox masked (ajax) as time with the mask format "99:99". Next to this box I need to present the entered value as decimal.

<asp:TextBox ID="time" runat="server" /> hh:mm ([time in decimal format])
<ajaxToolkit:MaskedEditExtender runat="server" Mask="99:99" TargetControlID="time" MaskType="Time" />

Solution

  • double decimalTime = DateTime.Now.Hour + (double)DateTime.Now.Minute/60
    

    Edit: Better way, using GenEric's TimeSpan idea:

    double hours = new TimeSpan(DateTime.Now.Hour, 
                                DateTime.Now.Minute, 
                                0).TotalHours;