在vb中排序日期

时间:2015-06-30 05:37:45

标签: vb.net list

我有几个日期形式的文件名(DD-MM-YYYY)。我已阅读所有名称并对其进行排序。但如果当天超过12天,应用程序将终止。我坚持这一部分。

在这里输入代码

For Each file As String In System.IO.Directory.GetFiles(directoryPath)
Dim name = System.IO.Path.GetFileNameWithoutExtension(file)
MsgBox(name)
list.Add(name)
Next
list.Sort(New Comparison(Of Date)(Function(x As Date, y As Date) y.CompareTo(x)))
ComboBox1.DataSource = list

我做了另一个过程

 Dim Dates As List(Of Date)
 For Each cbItem As Date In Me.ComboBox1.Items
 Dates = {
       Date.Parse(cbItem.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture))
        }.ToList
 Dates.Sort(New Comparison(Of Date)(Function(x As Date, y As Date) y.CompareTo(x)))
 Next
 ComboBox2.DataSource = Dates

2 个答案:

答案 0 :(得分:1)

您混淆了日期时间格式。

首先,您说您的格式为DD-MM-YYYY,但稍后在代码中尝试将其解析为MM/dd/yyyy

另外,一旦你遇到一天就得到例外的事实> 12表示您尝试将当天解析为月份,因为一年中只有12个月。

答案 1 :(得分:1)

试试这个:

START TRANSACTION;
INSERT INTO test (col1, col2, col3) VALUES ('1','1','1')
//also with COMMIT here
ROLLBACK;

注意:我已将该列表声明为Dim name As String Dim list As List(of DateTime) = new List(of DateTime) For Each file As String In System.IO.Directory.GetFiles(directoryPath) name = System.IO.Path.GetFileNameWithoutExtension(file) list.Add(Datetime.ParseExact(name, "dd-MM-yyyy", CultureInfo.InvariantCulture)) Next list.Sort(New Comparison(Of Date)(Function(x As Date, y As Date) y.CompareTo(x))) ComboBox1.DataSource = list 列表,并使用{{1}将文件名从DateTime转换为String DateTime方法。