2013-03-21 193 views
-2

我有一個文本文件,我已經提取了日期和時間。我需要它是這樣日期時間格式VB.NET

年月日HH毫米

我怎樣才能使之成爲這種格式。

我試圖

dim date as DateTime = line.Substring(line.Length - 19, 16) 
date= DateTime.ParseExact(date, "yyyy MM dd HH mm", CultureInfo.InvariantCulture) 

它給了一個錯誤

+4

傳遞一個字符串作爲ParseExact的第一個參數,而不是日期。將選項Strict On放在源代碼文件的頂部,這樣編譯器會告訴你這樣的錯誤。 – 2013-03-21 23:42:08

+0

你可以檢查這個http://www.vb6.us/tutorials/date-time-functions-visual-basic – Kasnady 2013-03-22 01:08:15

+0

在未來,我建議指定你收到的確切錯誤 – 2013-03-22 02:00:30

回答

2

從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) 

請注意,此處的日期將作爲無效標識符報告,因此您需要方括號。更好的解決方案是給變量賦予一個有意義的名稱。不幸的是,我不能提出一個建議,因爲你沒有在你的問題中提供任何背景。