在系统中比较不同date格式的date

我需要比较两个date。 他们是文本格式,他们看起来像30.05.2016,因为他们是从其他程序提取。 问题是,在一个系统上我得到不同的date格式(5/30/2016),比另一个(30/5/2016)。

我想知道我的想法是否正确,如果不是,我该怎么做。

首先我会检查一下我的格式。 如果(5/30/2016),那么我会做

1. Replace "." to "/" 2. CDate(value) 3. NumberFormat = "General" 4. Comparing date1 < date2 

如果(30/5/2016),那么我会做

 1. DateValue(Replace "." to "/") 2. NumberFormat = "General" 3. Comparing date1 < date2 

我仍然在思考如何编写这个代码,在这个阶段你的帮助会很好。

这假定date实际上是文本格式。 第一个UDF()处理美式风格的date:

 Public Function IsD1LessThanD2(d1 As String, d2 As String) As Boolean ' US Date format IsD1LessThanD2 = CDate(Replace(d1, ".", "/")) < CDate(Replace(d2, ".", "/")) End Function 

第二个UDF()处理欧洲格式:

 Public Function IsD1LessThanD2_E(d1 As String, d2 As String) As Boolean ' European Date format ary1 = Split(d1, ".") ary2 = Split(d2, ".") d1 = DateValue(ary1(1) & "/" & ary1(0) & "/" & ary1(2)) d2 = DateValue(ary2(1) & "/" & ary2(0) & "/" & ary2(2)) IsD1LessThanD2_E = d1 < d2 End Function 

在这里输入图像说明

您可以将这两个string格式化为date格式

 Dim date1, date2 As Date ' string1 in the format 5/30/2016 date1 = Format(string1, "mm/dd/yyyy") ' string2 in the format 30/5/2016 date2 = Format(string2, "dd/mm/yyyy") 

然后你可以简单地比较date。