我有一个文本文件,我提取了日期和时间。我需要这样
yyyy MM dd HH mm
我怎样才能把它变成这种格式。
我试过了
dim date as DateTime = line.Substring(line.Length - 19, 16)
date= DateTime.ParseExact(date, "yyyy MM dd HH mm", CultureInfo.InvariantCulture)
它给出了一个错误
从 MSDN,ParseExact有 3 个参数:
s
Type: System.String
A string that contains a date and time to convert.
format
Type: System.String
A format specifier that defines the required format of s.
For more information, see the Remarks section.
provider
Type: System.IFormatProvider
An object that supplies culture-specific format information about s.
所以它需要是这样的:
Dim [date] as DateTime =DateTime.ParseExact(line.Substring(line.Length - 19, 16),
"yyyy MM dd HH mm", CultureInfo.InvariantCulture)
请注意,此处的日期将被报告为无效标识符,因此您需要方括号。一个更好的解决方案是给你的变量一个有意义的名字。不幸的是,我不能推荐一个,因为你没有在你的问题中提供任何上下文。