ABCProgrammer
ABCProgrammer

Reputation:

How to display the current time and date in C#

How do you display the current date and time in a label in c#

Upvotes: 31

Views: 168142

Answers (10)

WILFRED DALIGDIG
WILFRED DALIGDIG

Reputation: 1

private void Form1_Load(object sender, EventArgs e)
{
    time();
    timer1.Interval = 1000;
    timer1.Start();
}
public void time()
{
        label1.Text = DateTime.Now.ToString("dd/MM/yyyy  hh:mm:ss tt");
}

private void timer1_Tick(object sender, EventArgs e)
{
    time();
}

this should update the time every second

Upvotes: 0

jopa
jopa

Reputation: 1

private void timer1_Tick(object sender, EventArgs e)
{
  if (true)
  {
    timer1.Interval = 1000;
    timer1.Start();
    label3.Text = DateTime.Now.ToString("dddd , MMM dd yyyy,hh:mm:ss");
  }
}

update time every second

Upvotes: -1

Madda96
Madda96

Reputation: 91

labelName.Text = DateTime.Now.ToString("dddd , MMM dd yyyy,hh:mm:ss");

Output:

][1

Upvotes: 9

dhilmathy
dhilmathy

Reputation: 2868

If you want to do it in XAML,

xmlns:sys="clr-namespace:System;assembly=mscorlib"
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}}"

With some formatting,

<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
                  StringFormat='{}{0:dd-MMM-yyyy hh:mm:ss}'}"

Upvotes: 4

Chris Pfohl
Chris Pfohl

Reputation: 19074

You'd need to set the label's text property to DateTime.Now:

labelName.Text = DateTime.Now.ToString();

You can format it in a variety of ways by handing ToString() a format string in the form of "MM/DD/YYYY" and the like. (Google Date-format strings).

Upvotes: 39

ExpertDeveloper
ExpertDeveloper

Reputation: 1

label1.Text = DateTime.Now.ToLongTimeString();//its for current date

label1.Text = DateTime.Now.ToLongDateString();//its for current time

Upvotes: -2

DevBW
DevBW

Reputation: 117

In WPF you'll need to use the Content property instead:

label1.Content = DateTime.Now.ToString();

Upvotes: 0

miky
miky

Reputation: 449

For time:

label1.Text = DateTime.Now.ToString("HH:mm:ss"); //result 22:11:45

or

label1.Text = DateTime.Now.ToString("hh:mm:ss tt"); //result 11:11:45 PM

For date:

label1.Text = DateTime.Now.ToShortDateString(); //30.5.2012

Upvotes: 17

Karthik Ratnam
Karthik Ratnam

Reputation: 3150

DateTime.Now.Tostring();

. You can supply parameters to To string function in a lot of ways like given in this link http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

This will be a lot useful. If you reside somewhere else than the regular format (MM/dd/yyyy)

use always MM not mm, mm gives minutes and MM gives month.

Upvotes: 2

Donut
Donut

Reputation: 112915

The System.DateTime class has a property called Now, which:

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.

You can set the Text property of your label to the current time like this (where myLabel is the name of your label):

myLabel.Text = DateTime.Now.ToString();

Upvotes: 16

Related Questions