如何在C#中显示当前时间和日期
如何在c#中的标签中显示当前日期和时间
How do you display the current date and time in a label in c#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在c#中的标签中显示当前日期和时间
How do you display the current date and time in a label in c#
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
您需要将标签的文本属性设置为
DateTime.Now
:您可以通过向
ToString()
传递以下形式的格式字符串,以多种方式对其进行格式化:“MM/DD/YYYY”
等。 (Google 日期格式字符串)。You'd need to set the label's text property to
DateTime.Now
: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).对于时间:
或
对于日期:
For time:
or
For date:
System.DateTime
类 有一个名为现在
,其中:您可以设置
文本 属性
更改为当前时间(其中
myLabel
是标签的名称):The
System.DateTime
class has a property calledNow
, which:You can set the
Text
property of your label to the current time like this (wheremyLabel
is the name of your label):输出:
Output:
如果您想在 XAML 中执行此操作,
使用一些格式,
If you want to do it in XAML,
With some formatting,
。您可以通过多种方式向 To string 函数提供参数,如本链接中给出的
http://www.geekzilla.co.uk/View00FF7904-B510 -468C-A2C8-F859AA20581F.htm
这将非常有用。如果您居住在常规格式 (MM/dd/yyyy) 以外的地方
始终使用 MM 而不是 mm,mm 表示分钟,MM 表示月份。
. 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.
在 WPF 中,您需要使用 Content 属性:
In WPF you'll need to use the Content property instead:
每秒更新一次
update time every second