Convert.ToDateTime:如何设置格式

时间:2021-05-21 21:32:39

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)

5 个解决方案

#1


16  

You should probably use either DateTime.ParseExact or DateTime.TryParseExact instead. 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.ParseExact或DateTime.TryParseExact。它们允许您指定特定格式。我个人更喜欢Try-versions,因为我认为它们会为错误情况生成更好的代码。

#2


11  

If value is a string in that format and you'd like to convert it into a DateTime object, you can use DateTime.ParseExact static method:

如果value是该格式的字符串,并且您想将其转换为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

#3


2  

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

DateTime没有格式。格式仅适用于将DateTime转换为字符串时,隐式显示您在表单,网页等上显示的值。

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

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

#4


0  

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"

测试将等于“12/2012”

Hope it helps!

希望能帮助到你!

Please read HERE.

请在这里阅读。

#5


0  

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

您可以使用Convert.ToDateTime,如何将Datetime字符串转换为当前文化日期时间字符串

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

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

#1


16  

You should probably use either DateTime.ParseExact or DateTime.TryParseExact instead. 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.ParseExact或DateTime.TryParseExact。它们允许您指定特定格式。我个人更喜欢Try-versions,因为我认为它们会为错误情况生成更好的代码。

#2


11  

If value is a string in that format and you'd like to convert it into a DateTime object, you can use DateTime.ParseExact static method:

如果value是该格式的字符串,并且您想将其转换为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

#3


2  

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

DateTime没有格式。格式仅适用于将DateTime转换为字符串时,隐式显示您在表单,网页等上显示的值。

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

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

#4


0  

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"

测试将等于“12/2012”

Hope it helps!

希望能帮助到你!

Please read HERE.

请在这里阅读。

#5


0  

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

您可以使用Convert.ToDateTime,如何将Datetime字符串转换为当前文化日期时间字符串

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

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