[英]Date time format in C#
I need "24/01/2012" but Why it always returns "24/1/2012" 我需要“ 24/01/2012”,但为什么总是返回“ 24/1/2012”
when I am using this 当我使用这个
txtFilingStartDate.Text = String.Format("{0:dd/MM/yyyy}", (SessionHelper.SearchFilingStartDate.Value.Day.ToString() + "/" + SessionHelper.SearchFilingStartDate.Value.Month.ToString() + "/" + SessionHelper.SearchFilingStartDate.Value.Year.ToString()));
只需这样做:
txtFilingStartDate.Text = String.Format("{0:dd/MM/yyyy}",SessionHelper.SearchFilingStartDate.Value);
SessionHelper.SearchFilingStartDate.Value
seems to be a DateTime value. SessionHelper.SearchFilingStartDate.Value
似乎是一个DateTime值。
Then pass it directly without all that string conversions for day, months and year. 然后直接将其传递,而无需进行日,月和年的所有字符串转换。
txtFilingStartDate.Text = String.Format("{0:dd/MM/yyyy}",
(SessionHelper.SearchFilingStartDate.Value);
The format string "dd/MM/yyyy"
contains enough information to allow the Format method to prepare the resulting string directly from your DateTime value 格式字符串
"dd/MM/yyyy"
包含足够的信息,以允许Format方法直接从您的DateTime值准备结果字符串
You don't need the String.Format
statement. 您不需要
String.Format
语句。 You can do it with the Date's ToString
method: 您可以使用Date的
ToString
方法执行此操作:
txtFilingStartDate = SessionHelper.SearchFilingStartDate.ToString.Value("dd/MM/yyyy");
There's a lot more information on the MSDN page for Custom Date and Time Format Strings 在MSDN页面上,有关自定义日期和时间格式字符串的更多信息
You are passing a string and the format you specified for it is unrecognized. 您正在传递字符串,并且无法识别为其指定的格式。 Assuming that this is a datetime you are passing, don't do all that conversion first:
假设这是您经过的日期时间,请不要先进行所有转换:
txtFilingStartDate.Text = String.Format("{0:dd/MM/yyyy}", SessionHelper.SearchFilingStartDate.Value)
When you format a string using a date/time formatting ( {0:dd/MM/yyyy}
) themethod is expecting an instance of DateTime
, you are passing it a string 当您使用日期/时间格式(
{0:dd/MM/yyyy}
)格式化字符串时,该方法期望使用DateTime
的实例,则将字符串传递给它
You probably want simply: 您可能只想:
txtFilingStartDate.Text = String.Format("{0:dd/MM/yyyy}",SessionHelper.SearchFilingStartDate.Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.