c# - 如何在标签中显示 DateTime 字段的短日期?

标签 c# winforms visual-studio sql-server-2008 datetime

我的 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/

相关文章:

C# 无法转换为 'Microsoft.EntityFrameworkCore.DbContextOptions'

c# - 我如何解决这个 lambda 表达式外部变量问题?

c# - 如果不重新启动,我无法在 Datagridview 中看到新元素

c# - 在 VS2017/2019 中构建时出现 StackOverflowException

.net - 基础设施许可证文件将自身添加到项目中?

c# - C#中位图图像的灰度变暗问题

c# - 用于查看 SharePoint 库中的照片的 Web 部件

c# - 带有用户输入验证的 KeyDown 事件

c# - App.config 中的 Winforms 连接字符串

c# - 如何在调试输出窗口中删除不需要的消息 - Xamarin on visual studio