日期和月份比较,不包括vb.net中的年份

时间:2016-01-05 10:23:21

标签: .net vb.net datetime comparison

给出以下信息

orderdate=05/01/2016
startday=24
startmonth=08
endday=2
endmonth=10

基本上,我们必须比较24 august10 october的销售期的订单日期。

超出24 august10 october期间的日期将是非销售期。

因此,对于任何给定的订单日期,我们必须计算它是在销售期还是​​非销售期。问题是我们没有提供仅年开始日,开始月,结束日和结束月。

还可以说我们是否使用日期时间start date=24/08/2016enddate=2/10/2016。这将需要当年,但通常是错误的。

应该{​​{1}}(由于非销售期将从2015年10月3日至2016年8月23日开始,因此销售期将开始并startdate=24/08/2015(我们正确无误)。

请告诉我是否有可能只比较没有年份的月份和日期来比较订单日期与开始日期,开始月份,结束日期和结束月份。

2 个答案:

答案 0 :(得分:1)

我认为您需要的是以下内容。它根据提供的月份和日期创建日期,然后调整它们以查找按时间顺序排列的历史日期:

    Private Function IsOrderInSalesPeriod(orderDate As Date, startMonth As Integer, startDay As Integer, endMonth As Integer, endDay As Integer) As Boolean

    Dim salesStartDate As New Date(DateTime.Now.Year, startMonth, startDay)
    Dim salesEndDate As New Date(DateTime.Now.Year, endMonth, endDay)
    'if the sales period is in the future it must be a year ahead
    If salesStartDate > DateTime.Now Then salesStartDate = salesStartDate.AddYears(-1)
    If salesEndDate > DateTime.Now Then salesEndDate = salesEndDate.AddYears(-1)
    'check that the start is before the end
    If salesStartDate > salesEndDate Then salesStartDate = salesStartDate.AddYears(-1)

    If orderDate >= salesStartDate AndAlso orderDate <= salesEndDate Then
        Return True
    Else
        Return False
    End If
End Function

答案 1 :(得分:0)

这可能有用,但你的问题不是很清楚。

Private Function InSalesPeriod(OrderDate As DateTime) As Boolean
    Dim salesStrt As New DateTime(OrderDate.Year, 8, 24)
    Dim salesEnd As New DateTime(OrderDate.Year, 10, 10)

    If OrderDate >= salesStrt AndAlso OrderDate <= salesEnd Then
        Return True
    Else
        Return False
    End If
End Function

这会检查订单日期是否在订单年份的日期之间。

相关问题