如何防止' DateTime.ParseExact(); '返回时间?
本文关键字:返回 时间 DateTime 何防止 ParseExact | 更新日期: 2023-09-27 18:18:09
我使用DateTime.ParseExact(date, "yyMMdd", CultureInfo.InvariantCulture).ToString();
将数字格式的日期更改为日期,我无法获得日期,但它也返回时间部分,但我希望它只是我的日期,下面是我的代码。
String date = TextBox3.Text.Trim();
Label9.Text = DateTime.ParseExact(date, "yyMMdd", CultureInfo.InvariantCulture).ToString();
for Input: 940226我得到输出: 02/26/1994 00:00:00我想删除时间部分
当然可以使用自定义日期/时间格式,但出于全球化的考虑,我建议您使用基于系统当前区域性信息返回日期字符串的方法之一。例如:
DateTime date = DateTime.ParseExact(strDate, "yyMMdd",CultureInfo.InvariantCulture);
Label9.Text = date.ToShortDateString();
我建议你先阅读MSDN上的自定义日期时间格式:
自定义日期和时间格式字符串
获取日期字符串最简单的方法是使用ToShortDateString()
。
使用dateTimePicker
然后将其Format
属性更改为short
,您将不必担心传递和格式化
要获得美国日期格式的输出,可以使用:
DateTime.ParseExact(date, "yyMMdd", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy");
默认ToString()
输出日期和时间。
如果要以当前区域性格式显示字符串,只需使用:
DateTime.ParseExact(date, "yyMMdd", CultureInfo.InvariantCulture).ToString("d");
由@bixarrio建议。我建议您阅读DateTime.ToString()
接受的不同快捷方式。