如何将两个字符串转换为DateTimes并在VB.NET中进行比较?

时间:2021-04-29 13:55:34

String comes back from the database with a format: '00/00/0000' I need to then compare it to a date that the user has entered in the same format. How do I make the conversion and compare the two dates?

字符串从数据库返回,格式为:'00 / 00/0000'我需要将其与用户以相同格式输入的日期进行比较。如何进行转换并比较两个日期?

5 个解决方案

#1


Use the static ParseExact method on the DateTime structure to convert the string. You will also pass the format you want, either dd/MM/yyyy or MM/dd/yyyy depending on what format you want (the example of 00/00/0000 doesn't give any indication of what format applies for you).

在DateTime结构上使用静态ParseExact方法来转换字符串。您还将传递所需的格式,dd / MM / yyyy或MM / dd / yyyy,具体取决于您想要的格式(00/00/0000的示例并未提供适用于您的格式的任何指示)。

#2


You can use

您可以使用


Dim dateA = DateTime.ParseExact(firstDateString, @"dd\/MM\/yyyy", Null)
Dim dateB = DateTime.ParseExact(secondDateString, @"dd\/MM\/yyyy", Null)
Dim areEqual = (dateA = dateB);

Assuming that your date format is day/month/year. If it's month/day/year just swap dd and MM

假设您的日期格式是日/月/年。如果它是月/日/年只是交换dd和MM

#3


Try something like this:

尝试这样的事情:

String.Compare("00/00/0000", dateTime.ToString("MM/dd/yyyy"))

But perhaps a better approach would be to do this:

但也许更好的方法是这样做:

DateTime.Equals(yourDateTime, DateTime.Parse(databaseDateTime));

#4


Try the following

请尝试以下方法

Dim date1 = CDate(firstDateString)
Dim date2 = CDate(secondDateString)
Dim comp = date1 = date2

#5


When you say compare, are you trying to analysis if the dates are the same (to the day) or within a period of a few days ? If to compare if the dates are the same then you can just compare the string or use the date.equals (as mentioned in posts before this one) , if you are trying to determine with a range you will have to use date compare

当你说比较时,你是否试图分析日期是相同的(到当天)还是在几天的时间内?如果要比较日期是否相同那么你可以只比较字符串或使用date.equals(如在此之前的帖子中所提到的),如果你试图确定一个范围你将不得不使用日期比较

        Dim lDate1 As String = "29/03/2009"
    Dim lDate2 As String = "30/03/2009"
    Dim lPeriod As Int16 = 7

    If lDate1 = lDate2 Then
        '** Dates the same
    End If

    If Date.Equals(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) Then
        '** The same
    End If

    If Date.Compare(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) > (lPeriod * -1) And Date.Compare(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) < lPeriod Then
        '** Within the period
    End If

#1


Use the static ParseExact method on the DateTime structure to convert the string. You will also pass the format you want, either dd/MM/yyyy or MM/dd/yyyy depending on what format you want (the example of 00/00/0000 doesn't give any indication of what format applies for you).

在DateTime结构上使用静态ParseExact方法来转换字符串。您还将传递所需的格式,dd / MM / yyyy或MM / dd / yyyy,具体取决于您想要的格式(00/00/0000的示例并未提供适用于您的格式的任何指示)。

#2


You can use

您可以使用


Dim dateA = DateTime.ParseExact(firstDateString, @"dd\/MM\/yyyy", Null)
Dim dateB = DateTime.ParseExact(secondDateString, @"dd\/MM\/yyyy", Null)
Dim areEqual = (dateA = dateB);

Assuming that your date format is day/month/year. If it's month/day/year just swap dd and MM

假设您的日期格式是日/月/年。如果它是月/日/年只是交换dd和MM

#3


Try something like this:

尝试这样的事情:

String.Compare("00/00/0000", dateTime.ToString("MM/dd/yyyy"))

But perhaps a better approach would be to do this:

但也许更好的方法是这样做:

DateTime.Equals(yourDateTime, DateTime.Parse(databaseDateTime));

#4


Try the following

请尝试以下方法

Dim date1 = CDate(firstDateString)
Dim date2 = CDate(secondDateString)
Dim comp = date1 = date2

#5


When you say compare, are you trying to analysis if the dates are the same (to the day) or within a period of a few days ? If to compare if the dates are the same then you can just compare the string or use the date.equals (as mentioned in posts before this one) , if you are trying to determine with a range you will have to use date compare

当你说比较时,你是否试图分析日期是相同的(到当天)还是在几天的时间内?如果要比较日期是否相同那么你可以只比较字符串或使用date.equals(如在此之前的帖子中所提到的),如果你试图确定一个范围你将不得不使用日期比较

        Dim lDate1 As String = "29/03/2009"
    Dim lDate2 As String = "30/03/2009"
    Dim lPeriod As Int16 = 7

    If lDate1 = lDate2 Then
        '** Dates the same
    End If

    If Date.Equals(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) Then
        '** The same
    End If

    If Date.Compare(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) > (lPeriod * -1) And Date.Compare(Date.ParseExact(lDate1, "dd/MM/yyyy", Nothing), Date.ParseExact(lDate2, "dd/MM/yyyy", Nothing)) < lPeriod Then
        '** Within the period
    End If