Convert time to decimal in .net

Is there an easy way to represent time (hh: mm) as a decimal value? Example: 01:08 should become 1.13.

I have asp: textbox masked (ajax) as the time with the mask format "99:99". Next to this field, I need to represent the entered value as a decimal.

<asp:TextBox ID="time" runat="server" /> hh:mm ([time in decimal format])
<ajaxToolkit:MaskedEditExtender runat="server" Mask="99:99" TargetControlID="time" MaskType="Time" />
+3
source share
3 answers
double decimalTime = DateTime.Now.Hour + (double)DateTime.Now.Minute/60

Edit: best way using the idea of ​​GenEric TimeSpan:

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

You can use the TotalHours property for TimeSpan, for example:

DateTime endHour = new DateTime(2010, 1, 2, 5, 30, 0);

double totalHours = new TimeSpan(endHour.Hour, endHour.Minute, 0).TotalHours;
+5
source

, TimeSpan, DateTime. , :

TimeSpan t = new TimeSpan(0, 1, 8);
decimal d = t.Minutes + (t.Seconds / 60m);
Console.WriteLine(d.ToString());

. m 60 .

You cannot connect this C # directly to the onblur text field, since it works on the server side. If you need to run this on the client side, you will either have to use the ajax callback to evaluate it, or use javascript to cut the line, then calculate each bit (minutes and seconds) separately, then output the result to a second text box or label.

+1
source

Source: https://habr.com/ru/post/1744141/


All Articles