将日期从6/05/2020格式转换为dd/MM/YYYY格式 [英] Convert Date from 6/05/2020 format to dd/MM/YYYY format

查看:72
本文介绍了将日期从6/05/2020格式转换为dd/MM/YYYY格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个小问题,尝试了很多事情之后我无法解决,所以它就在这里.....页面上有一个文本框,我在其中输入日期,并且我希望该日期在日期时间对象.

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.

例如: 日期输入:2020年6月5日(dd/MM/yyyy)应为相同格式,但在日期时间对象中访问时却已更改为(6.05.2020ie:MM/dd/yyyy格式).

for ex : date entd : 6 05 2020(dd/MM/yyyy) should be in same format when i am accessing it in date time object but it is getting changed to (6.05.2020ie: 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而不是2/11/2010)

dt should be (11/2/2010 rather then 2/11/2010)

@使用下面的代码后

@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); 

我在约会和约会中遇到的是6 05 2020 12:00:00 AM,应该是6/05/2020

What i am getting in edate and sdate is 6 05 2020 12:00:00 AM where it should be 6/05/2020

推荐答案

此值:"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.

DateTime没有格式.它仅表示日期和时间(在ISO日历中,可能在不同的时区中,但这是不同的事情).就像int一样-它不代表十进制整数"或十六进制整数"-只是特定范围内的整数.您可以格式化数字为十进制或十六进制,但是它本身并没有格式.

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进行解析,以在从文本框或

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.

这篇关于将日期从6/05/2020格式转换为dd/MM/YYYY格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆