0

I have a form which I need to show the exact time when my button was clicked. How may I "capture" the exact time (hh:mm:ss) when I pressed it?

(please, if the question is duplicated, inform me so I can delete this one)

MattDAVM
  • 505
  • 3
  • 6
  • 25

2 Answers2

3

Use DateTime.Now:

void myButton_Click(object sender, RoutedEventArgs e)
{
    // Captures the current time in a DateTime object.
    DateTime currentTime = DateTime.Now;

    // If you need a string, you can use the ToString method.
    string timeString = currentTime.ToString("hh:mm:ss");
}
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
2

Use DateTime.Now in your button clicked handler:

protected void btn_Clicked(object sender, EventArgs e)
{
    DateTime whenClicked = DateTime.Now;
}
itsme86
  • 19,266
  • 4
  • 41
  • 57