How to calculate difference between two dates in textboxes in C#. RRS feed

  • Question

  • Hai,

    Any Body please help me..

    am doing a project in ASP.NET

    am having 2 texboxes and 1 label,

    textbox1=get time in database(starting time ex:12:00)

    textbox2=Ending time ex:13:00

    i have to get difference in label ie,60(ie converted into minutes)

    What I have tried:

    function ActValuecheck() {
    var StartTime = $("#<%=lblstarttime.ClientID%>").text();
    var StartDate = $("#<%=hf_StartDate.ClientID%>").val();
    var EndTime = $("#<%=tp_ActEndtime.ClientID%>").val();
    var EndDate = $("#<%=dp_ReceiptDate.ClientID%>").val();
    var value = $("#<%=lblvalue.ClientID%>").text();

    var ST = StartTime.split(':');
    var ET = EndTime.split('-');
    var SD = StartDate.split('-');
    var ED = EndDate.split('-');
    var StartDate1 = new Date(SD[2], parseInt(SD[1]) - 1, SD[0], ST[0], ST[1], 0);
    var EndDate1 = new Date(ED[0], parseInt(ED[1]) - 1, ED[2], ET[3], ET[4], 0);
    //EndDate1.setHours(ET[3], ET[4], 0);
    var ActValue_InMin = (EndDate1 - StartDate1) / (60 * 1000);
    ActValue_InMin = parseFloat(ActValue_InMin).toFixed(0);
    document.getElementById("<%=lbl_Actvalue.ClientID%>").innerHTML = ActValue_InMin;
    document.getElementById("<%=lbl_Actvalue_hf.ClientID%>").value = ActValue_InMin;

    var Eff = (parseFloat(value) / ActValue_InMin) * 100;
    Eff = Eff.toFixed(2);

    <asp:label id="Label7" runat="server" text="StartTime:" font-bold="True">
    <asp:label id="lblstarttime" runat="server" text="" font-bold="True">
    <asp:hiddenfield id="hf_StartDate" runat="server">

    <asp:requiredfieldvalidator id="RequiredFieldValidator6" runat="server" forecolor="Red" controltovalidate="tp_ActEndtime" validationgroup="ReceiptSave" errormessage="*" font-bold="true">

    <asp:label id="Label2" runat="server" text="Act Value:">

    <%--<asp:textbox id="txtActvalue" enabled="false" runat="server" class="form-control input-md">--%>
    <asp:label id="lbl_Actvalue" runat="server" text="">
    <asp:hiddenfield id="lbl_Actvalue_hf" runat="server">

    <asp:label id="Label3" runat="server" text="Efficiency (in %):">
    • Moved by CoolDadTx Friday, July 5, 2019 1:44 PM ASP.NET related
    Friday, July 5, 2019 6:53 AM

All replies

  • Hi Deepa,

    Thank you for posting here.

    According to your description, I suggest you could try to use monment.js to convert to the “13:00” to datettime  and jquery to achieve your requirement.

    Details, you could refer to below codes:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavascriptDifferentTime.aspx.cs" Inherits="AspNetNormalIssue.Webform.JavascriptDifferentTime" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="../Scripts/jquery-1.9.1.min.js"></script>
        <script src="../Scripts/moment.min.js"></script>
            $(function () {           
                $("#Button1").click(function (e) {
                    var dt1 = moment($("#TextBox1").val(), 'HH:mm');
                    var dt2 = moment($("#TextBox2").val(), 'HH:mm');
                    var __duration = moment.duration(moment(dt2).diff(dt1)).asMinutes();
        <form id="form1" runat="server">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                Result: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="Button" />


    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 5, 2019 7:53 AM
  • Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, July 5, 2019 1:44 PM