问题描述
我面临的一个小问题,我还没有那么这里去尝试这么多事情之后,能够.....有一个在我的网页中,我正在输入日期的文本框,我想在一个日期该日期对象。
I am facing a small issue which i am not able after trying so many things so here it goes ..... There is a text box in my page in which i am entering date and i want that date in a datetime object.
为前:
日期进入:11/2/2010(DD / MM / YYYY)应该是相同的格式,当我在约会时对象访问它,但它是越来越变为(2011年2月11日即:MM / dd / yyyy格式)
for ex :date entered : 11/2/2010 (dd/MM/yyyy) should be in same format when i am accessing it in date time object but it is getting changed to (2/11/2011 ie: MM/dd/yyyy format).
我希望我在这里决策意识我要的就是这样的一些东西.....
i hope i am making sense here all i want is some thing like this.....
DateTime dt = convert.ToDateTime(txtDate.Text);
DT应该是(11/2/2010而不是2010年2月11日)
dt should be (11/2/2010 rather then 2/11/2010)
@oded使用以下code后
@oded after using the following code
DateTime sDate, eDate = new DateTime();
//修改为我所用的日期。
DateTime.TryParseExact(txtFrom.Text,DD / MM / YYYY,CultureInfo.InvariantCulture,DateTimeStyles.None,出SDATE);
//To modify dates for our use. DateTime.TryParseExact(txtFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out sDate);
DateTime.TryParseExact(txtFrom.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out eDate);
我所得到的EDATE和SDATE是1/1/0001 12:00:00 AM它应该是3/11/2011。
What i am getting in edate and sdate is 1/1/0001 12:00:00 AM where it should be 3/11/2011.
推荐答案
编辑:此值:11/2/2010不匹配的格式为DD / MM / YYYY。它的格式为D / M / YYYY匹配 - 为DD / MM / YYYY它应该是11/02/2010
This value: "11/2/2010" doesn't match the format "dd/MM/yyyy". It matches the format "d/M/yyyy" - for "dd/MM/yyyy" it should be "11/02/2010".
这就是为什么 TryParseExact
失败你。你需要选择合适的格式模式。
That's why TryParseExact
is failing for you. You need to pick the right format pattern.
A 的DateTime
值的没有格式的。它只是重新presents日期和时间(在ISO日历,并有可能在不同的时区,但是那是另一回事)。这就像一个 INT
- 它不会再present十进制整数或十六进制整数 - 这只是一个特定范围内的整数。你可以的格式的一个数字为十进制或十六进制,但它并不具有的格式。
A DateTime
value doesn't have a format. It just represents date and time (in the ISO calendar, and possibly in different time zones, but that's a different matter). It's like an int
- it doesn't represent "a decimal integer" or "a hex integer" - it's just an integer within a particular range. You can format a number as decimal or hex, but it doesn't inherently have a format.
这听起来像你应该 ParseExact
解析它以指定格式转换时的从的文本框,或者可能是<$c$c>TryParseExact$c$c>:
It sounds like you should parse it with ParseExact
to specify the format when converting from the textbox, or probably TryParseExact
:
// This is assuming you're absolutely sure of the format used. This is *not*
// necessarily the user's preferred format. You should think about where your
// data is coming from.
DateTime date;
if (DateTime.TryParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
// Okay, successful parse. We now have the date. Use it, avoiding formatting
// it back to a string for as long as possible.
}
您应该保持该值作为的DateTime
用于所有目的的除了的给它回给用户 - 此时你可能需要使用他们的文化背景。
You should keep that value as DateTime
for all purposes except giving it back to a user - at which point you may well want to use their cultural settings.
在特别的,如果你存储在你的数据库的值不应的将其转换为文本,并将其包含在SQL语句 - 这就是自找麻烦。相反,使用参数化的SQL语句,并将其设置为参数值,仍作为的DateTime
。
In particular, if you're storing the value in a database you should not convert it to text and include it in a SQL statement - that's asking for trouble. Instead, use a parameterized SQL statement and set it as the parameter value, still as a DateTime
.
这篇关于从MM / DD / YYYY格式转换日期为DD / MM / YYYY格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!