英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论