Converting a string to date in c#

huangapple go评论61阅读模式
英文:

Converting a string to date in c#

问题

from a file i read a string like this: "Nov 4 07:27:27 2022 GMT". I try to convert this to german Datetime, later it will be stored in a SQL-table.
Currently i do the conversion like this:

MyTmpString = "Nov  4 07:27:27 2022 GMT";
string format;
CultureInfo provider = CultureInfo.InvariantCulture;
format = "MMM  d HH:mm:ss yyyy GMT";
try {
    Datum_von = DateTime.ParseExact(MyTmpString, format, provider);
    Console.WriteLine("{0} converts to {1}.", MyTmpString, Datum_von.ToString());
}
catch (FormatException) {
    Console.WriteLine("{0} is not in the correct format.", MyTmpString);
}

This works for that specific case. I already run into problems if day as more than 1 digit. In this case i have to use a format like this:

format = "MMM dd HH:mm:ss yyyy GMT";

May be the string occurs in more variants currently i dont know. i dont want to create a format and try-catch for every possibility. Iam looking for a kind of universal conversionmethod to have a german date at the end.
What should i do here, do you have any recommendations?

Thank you,
Hans

英文:

from a file i read a string like this: "Nov 4 07:27:27 2022 GMT". I try to convert this to german Datetime, later it will be stored in a SQL-table.
Currently i do the conversion like this:

MyTmpString = "Nov  4 07:27:27 2022 GMT";
string format;
CultureInfo provider = CultureInfo.InvariantCulture;
format = "MMM  d HH:mm:ss yyyy GMT";
try {
    Datum_von = DateTime.ParseExact(MyTmpString, format, provider);
    Console.WriteLine("{0} converts to {1}.", MyTmpString, Datum_von.ToString());
}
catch (FormatException) {
    Console.WriteLine("{0} is not in the correct format.", MyTmpString);
}

This works for that specific case. I already run into problems if day as more than 1 digit. In this case i have to use a format like this:

format = "MMM dd HH:mm:ss yyyy GMT";

May be the string occurs in more variants currently i dont know. i dont want to create a format and try-catch for every possibility. Iam looking for a kind of universal conversionmethod to have a german date at the end.
What should i do here, do you have any recommendations?

Thank you,
Hans

答案1

得分: 2

根据Hans Kesting的建议,ParseExact-d格式不应关心日期是1位数还是2位数。你的问题在于额外的空格。
此外,不要依赖捕获异常,这在性能上很昂贵,你应该使用TryParseExact。它甚至有一个选项,允许你忽略字符串中的多余空白:

var myTmpString = "Nov    14    07:27:27   2022 GMT";
// 或者 var myTmpString = "Nov    4    07:27:27   2022 GMT";

System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string format = "MMM d HH:mm:ss yyyy GMT";
bool result = DateTime.TryParseExact(myTmpString, format, provider, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out DateTime datum_von);
if ( result )
{
    Console.WriteLine("{0} converts to {1}.", myTmpString, datum_von.ToString());
}
else
{
    Console.WriteLine("{0} is not in the correct format.", myTmpString);
}

如果TryParseExact返回true,你可以使用out参数的值,否则解析失败。

英文:

As Hans Kesting suggests, ParseExact's -d format shouldn't care if days are 1 or 2 digits. Your problem is the extra spaces.
Also instead of relying on catching an exception, which is expensive performance-wise, you should use TryParseExact. It even has an option that allows you to ignore superfluous white space in the string:

var myTmpString = "Nov    14    07:27:27   2022 GMT";
// Or var myTmpString = "Nov    4    07:27:27   2022 GMT";

System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string format = "MMM d HH:mm:ss yyyy GMT";
bool result = DateTime.TryParseExact(myTmpString, format, provider, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out DateTime datum_von);
if (result)
{
    Console.WriteLine("{0} converts to {1}.", myTmpString, datum_von.ToString());
}
else
{
    Console.WriteLine("{0} is not in the correct format.", myTmpString);
}

If TryParseExact returns true, you can use the value from the out parameter, otherwise parsing has failed.

huangapple
  • 本文由 发表于 2023年2月6日 18:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定