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

发布于 2024-10-06 15:46:14 字数 26 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

若言繁花未落 2024-10-13 15:46:14

您需要将标签的文本属性设置为 DateTime.Now

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

您可以通过向 ToString() 传递以下形式的格式字符串,以多种方式对其进行格式化: “MM/DD/YYYY” 等。 (Google 日期格式字符串)。

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).

楠木可依 2024-10-13 15:46:14

对于时间:

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

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

对于日期:

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

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
凡尘雨 2024-10-13 15:46:14

System.DateTime 有一个名为现在,其中:

获取设置为的 DateTime 对象此计算机上的当前日期和时间,以当地时间表示。

您可以设置文本 属性更改为当前时间(其中 myLabel 是标签的名称):

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

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();
一影成城 2024-10-13 15:46:14
labelName.Text = DateTime.Now.ToString("dddd , MMM dd yyyy,hh:mm:ss");

输出:

] [1

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

Output:

][1

叹沉浮 2024-10-13 15:46:14

如果您想在 XAML 中执行此操作,

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

使用一些格式,

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

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}'}"
孤城病女 2024-10-13 15:46:14

DateTime.Now.Tostring();

。您可以通过多种方式向 To string 函数提供参数,如本链接中给出的
http://www.geekzilla.co.uk/View00FF7904-B510 -468C-A2C8-F859AA20581F.htm

这将非常有用。如果您居住在常规格式 (MM/dd/yyyy) 以外的地方

始终使用 MM 而不是 mm,mm 表示分钟,MM 表示月份。

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.

冰葑 2024-10-13 15:46:14

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

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

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

label1.Content = DateTime.Now.ToString();
青丝拂面 2024-10-13 15:46:14
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");
  }
}

每秒更新一次

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

不必了 2024-10-13 15:46:14
label1.Text = DateTime.Now.ToLongTimeString();//its for current date

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

label1.Text = DateTime.Now.ToLongDateString();//its for current time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文