C# Convert.ToDateTime:如何设置格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15203534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:27:08  来源:igfitidea点击:

Convert.ToDateTime: how to set format

c#.netdatetime

提问by Refael

I use convert like:

我使用转换如:

Convert.ToDateTime(value)

but i need convert date to format like "mm/yy".
I'm looking for something like this:

但我需要将日期转换为“mm/yy”等格式。
我正在寻找这样的东西:

var format = "mm/yy";
Convert.ToDateTime(value, format)

采纳答案by Fredrik M?rk

You should probably use either DateTime.ParseExactor DateTime.TryParseExactinstead. They allow you to specify specific formats. I personally prefer the Try-versions since I think they produce nicer code for the error cases.

您可能应该使用其中之一DateTime.ParseExactDateTime.TryParseExact代替。它们允许您指定特定格式。我个人更喜欢Try-versions,因为我认为它们为错误情况生成更好的代码。

回答by MarcinJuraszek

If valueis a stringin that format and you'd like to convert it into a DateTimeobject, you can use DateTime.ParseExactstatic method:

如果valuestring那种格式并且您想将其转换为DateTime对象,则可以使用DateTime.ParseExact静态方法:

DateTime.ParseExact(value, format, CultureInfo.CurrentCulture);

Example:

例子:

string value = "12/12";
var myDate = DateTime.ParseExact(value, "MM/yy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);

Console.WriteLine(myDate.ToShortDateString());

Result:

结果:

2012-12-01

回答by D Stanley

DateTimedoesn't have a format. the format only applies when you're turning a DateTimeinto a string, which happens implicitly you show the value on a form, web page, etc.

DateTime没有格式。该格式仅在您将 aDateTime转换为字符串时才适用,这种情况隐含地发生在您在表单、网页等上显示该值时。

Look at whereyou're displaying the DateTime and set the format there (or amend your question if you need additional guidance).

查看您显示 DateTime 的位置并在那里设置格式(或者如果您需要其他指导,请修改您的问题)。

回答by jomsk1e

How about this:

这个怎么样:

    string test = "01-12-12";
    try{
         DateTime dateTime = DateTime.Parse(test);
         test = dateTime.ToString("dd/yyyy");
    }
    catch (FormatException exc)
    {
        MessageBox.Show(exc.Message);
    }

Where test will be equal to "12/2012"

其中 test 将等于“12/2012”

Hope it helps!

希望能帮助到你!

Please read HERE.

请阅读这里

回答by Michael Freidgeim

You can use Convert.ToDateTime is it is shown at How to convert a Datetime string to a current culture datetime string

您可以使用 Convert.ToDateTime 是否显示在如何将日期时间字符串转换为当前区域性日期时间字符串

DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;

var result = Convert.ToDateTime("12/01/2011", usDtfi)

回答by pravin...

You can use this also.

你也可以使用这个。

 dtFromDate = Convert.ToDateTime(DateTime.ParseExact(fromDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
                      .ToString("MM/dd/yyyy", CultureInfo.InvariantCulture));