Get current DateTime on ASP.Net Button Click using C#

muhammad12
 
on May 06, 2022 11:08 PM
1044 Views

Hi dharmendr sir,

I have a button showing text START when I click on it I shows me current time in textview. (I consider this time as start time).

Than convert the text on Button into STOP. When I click on it again show me current time on another textview (I consider this time as stop time).

And then calculate the difference between these two times.

I want this same working for multiple times.

I mean First button show Text ENTERS. When I click on it should show current time in textview. (I will consider this time as Enter time).

Than convert the text on Button into START. When I click on it again show me current time on another textview (I will consider this time as start time).

Than convert the text on Button into STOP. When I click on it again show me current time on another textview (I will consider this time as STOP time).

And last

Than convert the text on Button into EXIT. When I click on it again show me current time on another textview (I will consider this time as Exit time).

Here is my code in java language.

Please help me sir please.

btn_start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
 
        if (isStarted){
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
            String time = format.format(calendar.getTime());
            tvend.setText(time);
            try {
                Date date1 = format.parse(tvend.getText().toString());
                Date date2 = format.parse(tvstarttime.getText().toString());
                long mills = date1.getTime() - date2.getTime();
                Log.v("Data1", ""+date1.getTime());
                Log.v("Data2", ""+date2.getTime());
                int hours = (int) (mills/(1000 * 60 * 60));
                int mins = (int) (mills % (1000*60*60));
 
                String diff = hours + ":" + mins;
                diffence.setText(diff);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            btn_start.setText("START");
        }else {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
            String time1 = format.format(calendar.getTime());
 
            tvstarttime.setText(time1);
            btn_start.setText("STOP");
        }
        isStarted = !isStarted;
    }
});
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on May 09, 2022 11:55 AM
on Jul 08, 2022 06:26 AM

Hi muhammad12,

Use switch case. Based on the Button Text Action set the current DateTime and alter the Button Text.

Check below example.

HTML

<table>
    <tr>
        <td>Enter</td>
        <td>
            <asp:Label ID="lblEnter" runat="server" /></td>
    </tr>
    <tr>
        <td>Start</td>
        <td>
            <asp:Label ID="lblStart" runat="server" /></td>
    </tr>
    <tr>
        <td>Stop</td>
        <td>
            <asp:Label ID="lblStop" runat="server" /></td>
    </tr>
    <tr>
        <td>Exit</td>
        <td>
            <asp:Label ID="lblExit" runat="server" /></td>
    </tr>
</table>
<asp:Button ID="btnAction" Text="Enter" runat="server" OnClick="btnAction_Click" />

Code

protected void btnAction_Click(object sender, EventArgs e)
{
    string action = btnAction.Text.Trim().ToLower();
    switch (action)
    {
        case "enter":
            btnAction.Text = "Start";
            lblEnter.Text = DateTime.Now.ToString();
            break;
        case "start":
            btnAction.Text = "Stop";
            lblStart.Text = DateTime.Now.ToString();
            break;
        case "stop":
            btnAction.Text = "Exit";
            lblStop.Text = DateTime.Now.ToString();
            break;
        case "exit":
            btnAction.Text = "Enter";
            lblExit.Text = DateTime.Now.ToString();
            break;
        default:
            break;
    }
}

Output

Enter 5/9/2022 11:23:55 PM
Start 5/9/2022 11:23:56 PM
Stop 5/9/2022 11:23:57 PM
Exit 5/9/2022 11:23:58 PM