如何在标签中显示DateTime字段的短日期? [英] How to display short date from DateTime field in label?

查看:154
本文介绍了如何在标签中显示DateTime字段的短日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQL Server数据库中有一个DateTime字段,我想在WinForm(C#)的标签上显示短日期格式的日期。我仍然得到06/01/2013 12:00:00,即使使用:

I have a DateTime field in my SQL Server DB, and I would like to display just the date in short date form on a label in a WinForm (C#). I still get 06/01/2013 12:00:00, though, even when using:

label_date.Text = myDateTimeField.Date.ToString();

为了提取时间供以后使用,我使用:

To extract the time for later use, I use:

label_time.Text = myDateTimeField.TimeOfDay.ToString();

,工作正常。

任何人都知道如何获取日期单独

Anyone know how to get the date out alone?

推荐答案

您可以使用 DateTime.ToShortDateString

You can use DateTime.ToShortDateString:

label_date.Text = myDateTimeField.ToShortDateString();

备注:


当前DateTime对象的值使用模式进行格式化.aspxrel =nofollow> DateTimeFormatInfo.ShortDatePattern 属性
与当前线程文化相关联。返回值为
,与通过指定d标准
具有ToString(String)方法的DateTime格式字符串相同的值。

The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture. The return value is identical to the value returned by specifying the "d" standard DateTime format string with the ToString(String) method.

ToShortDateString方法返回的字符串是
对文化敏感。它反映了当前
文化的 DateTimeFormatInfo 对象。例如,对于en-US
文化,标准的短日期模式是M / d / yyyy;对于de-de
文化,它是dd.MM.yyyy;对于ja-JP文化,它是yyyy / M / d。
特定计算机上的特定格式字符串也可以定制为
,以便与标准的短日期格式
字符串不同。

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.

这篇关于如何在标签中显示DateTime字段的短日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆