I am facing a couple of challenges:
-I am trying to calculate the time duration in hours between two military times input by the user in two textboxes. The result should be in quarter-hour intervals like 2.25 hours, 2.75 hours, etc.
-The current calculation I have is showing a formatting error upon submission.
I have a form where I need to compute the difference in hours between a start-time and an end-time, entered in military (24-hour) format. Since employees are paid in quarter-hour increments, the output should display the total hours down to the quarter-hour (e.g., 4.25 hrs, 4.75 hrs, etc).
Due to the structure of the form, the start-date/time and end-date/time are in separate textboxes. Additionally, some browsers do not support input type="time", so I had to use textboxes with type="number" instead.
Here is the ASP code snippet:
<label for="tbBeginningTime">beginning time</label>
<asp:TextBox runat="server" ID="tbBeginningTime" type="number" AllowNegatives="false" onblur="javascript:hoursCalc();"></asp:TextBox>
<label for="tbEndingTime">ending time</label>
<asp:TextBox runat="server" ID="tbEndingTime" type="number" AllowNegatives="false" onblur="javascript:hoursCalc();"></asp:TextBox>
<label for="tbTotalTime">total time</label>
<asp:TextBox runat="server" ID="tbTotalTime" type="number" ReadOnly="true" AllowNegatives="false"></asp:TextBox>
I have used JavaScript to perform a basic calculation to determine the hours, but I am struggling to handle the quarter-hour decimal points (.25, .50, .75). I attempted to use modulus to separate out the decimals and also tried splitting them into an array, but the correct result did not appear in the tbTotalTime textbox.
Here is the relevant JavaScript code:
function hoursCalc () {
var startTime = document.getElementById('<%= tbBeginningTime.ClientID %>');
var endTime = document.getElementById('<%= tbEndingTime.ClientID %>');
var totalTime = document.getElementById('<%= tbTotalTime.ClientID %>');
var t1 = 0;
var t2 = 0;
if (startTime.value != "") t1 = startTime.value;
if (endTime.value != "") t2 = endTime.value;
if (endTime.value < startTime.value) t2 = parseInt(endTime.value) + 2400;
totalTime.value = parseInt((parseInt(t2) - parseInt(t1)) / 100);
}
While this calculates the hours and displays it in the tbTotalTime textbox, I encounter an error when trying to submit the form with a parameterized SQL insert statement, stating "Input string was not in a correct format."
Below is the C# code I am using:
protected void btnSaveClick(object sender, EventArgs e)
{
string connstring = ConfigurationManager.ConnectionStrings["TimeHubDBCS"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connstring))
{
try
{
SqlCommand cmd = new SqlCommand("spInsertRequestCO", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@BeginningTime", SqlDbType.Time).Value = DateTime.ParseExact(tbBeginningTime.Text, "HHmm", null).TimeOfDay;
cmd.Parameters.Add("@EndingTime", SqlDbType.Time).Value = DateTime.ParseExact(tbEndingTime.Text, "HHmm", null).TimeOfDay;
cmd.Parameters.Add("@TotalTime", SqlDbType.Int).Value = int.Parse(tbTotalTime.Text);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
SuccessMessage = "Request Saved Successfully";
DisplaySuccessDialog(null, null);
ConfigureButtons(null, null);
}
catch (Exception ex)
{
PopupTitle = "Save Error: ";
ErrorMessage = ex.Message;
DisplayErrorDialog(null, null);
}
}
}
I understand that this might be a complex question, and I might receive some downvotes for it. However, I am seeking any suggestions or guidance to streamline this process.