我的 SQL Server 数据库中有一个 DateTime 字段,我想在 WinForm (C#) 的标签上仅以短日期格式显示日期。不过,我仍然得到 06/01/2013 12:00:00,即使使用:
label_date.Text = myDateTimeField.Date.ToString();
为了提取时间供以后使用,我使用:
label_time.Text = myDateTimeField.TimeOfDay.ToString();
而且效果很好。
有人知道如何单独约会吗?
最佳答案
您可以使用 DateTime.ToShortDateString
:
label_date.Text = myDateTimeField.ToShortDateString();
备注:
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.
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.
关于c# - 如何在标签中显示 DateTime 字段的短日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14898079/