如何在C#中显示当前时间和日期

时间:2023-02-06 15:42:30

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

如何在c#中显示标签中的当前日期和时间

7 个解决方案

#1


23  

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

您需要将标签的text属性设置为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).

您可以通过以“MM / DD / YYYY”等形式处理ToString()格式字符串,以各种方式对其进行格式化。 (谷歌日期格式字符串)。

#2


14  

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

System.DateTime类有一个名为Now的属性,它具有:

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

获取一个DateTime对象,该对象在此计算机上设置为当前日期和时间,表示为本地时间。

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

您可以将标签的Text属性设置为当前时间(myLabel是标签的名称):

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

#3


8  

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

#4


2  

DateTime.Now.Tostring();

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

。你可以通过很多方式为To string函数提供参数,如链接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)

这将非常有用。如果你住在除常规格式之外的其他地方(MM / dd / yyyy)

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

使用总是MM不是mm,mm给出分钟而MM给出月份。

#5


0  

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

在WPF中,您需要使用Content属性:

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

#6


0  

If you want to do it in XAML,

如果你想在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}'}"

#7


-1  

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

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

#1


23  

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

您需要将标签的text属性设置为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).

您可以通过以“MM / DD / YYYY”等形式处理ToString()格式字符串,以各种方式对其进行格式化。 (谷歌日期格式字符串)。

#2


14  

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

System.DateTime类有一个名为Now的属性,它具有:

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

获取一个DateTime对象,该对象在此计算机上设置为当前日期和时间,表示为本地时间。

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

您可以将标签的Text属性设置为当前时间(myLabel是标签的名称):

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

#3


8  

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

#4


2  

DateTime.Now.Tostring();

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

。你可以通过很多方式为To string函数提供参数,如链接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)

这将非常有用。如果你住在除常规格式之外的其他地方(MM / dd / yyyy)

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

使用总是MM不是mm,mm给出分钟而MM给出月份。

#5


0  

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

在WPF中,您需要使用Content属性:

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

#6


0  

If you want to do it in XAML,

如果你想在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}'}"

#7


-1  

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

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