C# 如何将字符串[]数组放入DateTime?
我是C#的新手 我有不同的文件名。例如: C:\Test\ABCD\Warranty\u 2018\u 02\u 12\u 13\u 25\u 13.743.xml 从这个名字我想知道日期,比如2018年2月12日13:25:13 所以这些文件从来都不一样 我的代码:C# 如何将字符串[]数组放入DateTime?,c#,datetime,C#,Datetime,我是C#的新手 我有不同的文件名。例如: C:\Test\ABCD\Warranty\u 2018\u 02\u 12\u 13\u 25\u 13.743.xml 从这个名字我想知道日期,比如2018年2月12日13:25:13 所以这些文件从来都不一样 我的代码: public string GetCreationDate(string fileResult) { int index = fileResult.IndexOf("_");
public string GetCreationDate(string fileResult)
{
int index = fileResult.IndexOf("_");
if (index != -1)
{
string date = fileResult.Substring(index + 1, 20);
char[] delimiter = new char[] { '_' };
string[] dateWithoutLines = date.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
Array.Reverse(dateWithoutLines, 0, 3);
//Here is the error
//Guess it's because of the 'ToString()'
DateTime dateTime = DateTime.ParseExact(dateWithoutLines.ToString(), "dd/MM/yyyy hh:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
return dateTime.ToString();
}
return null;
}
在调试器中,我现在在dateWithoutLines中有6个具有正确日期的字符串。比如“12”“02”“2018”
但是它说这不是一个正确的日期时间格式。好的,但是如果我删除
ToString()
它表示无法将字符串[]转换为字符串。那么这里出了什么问题 对象实例
dateWithoutLines
是字符串数组,而不是单个字符串,因此不能使用方法ToString()
所以我相信你想要的是:
foreach (string date in dateWithoutLines)
{
DateTime dateTime = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
}
请注意,由于您有一个字符串数组,date是一个字符串对象,因此无需为每个
date
string实例调用ToString()
方法对象实例dateWithoutLines
是一个字符串数组,而不是单个字符串,因此无法使用方法ToString()
所以我相信你想要的是:
foreach (string date in dateWithoutLines)
{
DateTime dateTime = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
}
请注意,由于您有一个字符串数组,date是一个字符串对象,因此无需为每个
date
字符串实例调用ToString()
方法dateWithoutLines
是一个string[]
。string[]
的ToString
实现产生的“System.string[]”不是有效日期。此外,DateTime.Parse
不接受字符串[]
,而只接受字符串
如果文件名中的日期格式始终相同,则可以使用string.format
var formattedDate = string.Format("{2}.{1}.{0} {3}:{4}:{5}", dateWithoutLines);
大括号中的数字表示按索引传递的数组中的对象,即在您的示例中,{2}
将替换为12
,{1}
替换为02
,依此类推请注意我使用了原始顺序的索引,而不是反向数组
由于您正在解析日期以对其进行格式化,因此不需要解析,因为它已经格式化。
dateWithoutLines
是一个字符串[]
。string[]
的ToString
实现产生的“System.string[]”不是有效日期。此外,DateTime.Parse
不接受字符串[]
,而只接受字符串
如果文件名中的日期格式始终相同,则可以使用string.format
var formattedDate = string.Format("{2}.{1}.{0} {3}:{4}:{5}", dateWithoutLines);
大括号中的数字表示按索引传递的数组中的对象,即在您的示例中,{2}
将替换为12
,{1}
替换为02
,依此类推请注意我使用了原始顺序的索引,而不是反向数组
由于您正在解析日期以对其进行格式化,因此不需要解析,因为它已经格式化。无需拆分原始字符串、反转、合并等等 让
DateTime.TryParseExact
为您完成所有工作:
也可以考虑返回一个空的日期时间(<代码>日期时间> <代码>),而不是一个字符串:
public DateTime? GetCreationDate(string fileResult)
{
int index = fileResult.IndexOf("_");
if (index <= 0)
return null;
// check for string length, you don't want the following call to fileResult.Substring to throw an exception
if (fileResult.Length < index+20)
return null;
string date = fileResult.Substring(index + 1, 20);
DateTime dt;
if (DateTime.TryParseExact(date, "yyyy_MM_dd__HH_mm_ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
return dt;
return null;
}
公共日期时间?GetCreationDate(字符串文件结果)
{
int index=fileResult.IndexOf(“”);
if(index无需拆分原始字符串、反转、合并等
让DateTime.TryParseExact
为您完成所有工作:
也可以考虑返回一个空的日期时间(<代码>日期时间> <代码>),而不是一个字符串:
public DateTime? GetCreationDate(string fileResult)
{
int index = fileResult.IndexOf("_");
if (index <= 0)
return null;
// check for string length, you don't want the following call to fileResult.Substring to throw an exception
if (fileResult.Length < index+20)
return null;
string date = fileResult.Substring(index + 1, 20);
DateTime dt;
if (DateTime.TryParseExact(date, "yyyy_MM_dd__HH_mm_ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
return dt;
return null;
}
公共日期时间?GetCreationDate(字符串文件结果)
{
int index=fileResult.IndexOf(“”);
如果(索引是的,但这里有相同的错误,它说DateTime的格式不正确。是的,但这里有相同的错误,它说DateTime的格式不正确。啊,谢谢!这很有效!所以这里我不必把字符串给DateTime构造函数?你可以将字符串格式化为DateTime.Parse
明白吗s并返回一个日期时间
而不是字符串
,如果您对日期做的更多,而不是输出它,这会更好。啊,谢谢!这很有效!因此我不必将字符串交给日期时间构造函数?您可以将字符串格式化为日期时间。解析理解并返回日期时间
而不是字符串
,如果您对日期做的更多,而不是输出日期,这会更好。