簡體   English   中英

在vb.net中不包含年份的日和月比較

[英]Day and month comparison without inluding year in vb.net

給出以下信息

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

基本上,我們必須比較24 august 10 october 24 august10 october銷售訂單日期。

24 august 10 october 24 august10 october日期間的日期將是非銷售期。

因此,對於任何給定的訂單日期,我們必須計算它是處於銷售期還是​​非銷售期。 問題是我們不只提供年份的開始日期,開始月份,結束日期和結束月份。

還可以說如果我們使用Date time,則將start date=24/08/2016 enddate=2/10/2016enddate=2/10/2016 需要一年的時間,但通常是錯誤的。

該日期應為startdate=24/08/2015 (因為非銷售期將從2015年10月3日開始至2016年8月23日,之后該銷售期將開始並結束日期enddate=2/10/2016 (我們逐漸正確)。

請告訴我是否有可能只比較月份和日期,而不比較年份,以便將訂單日期與給定的開始日期,開始月份,結束日期和結束月份進行比較。

我認為您需要的是以下內容。 它根據提供的月份和日期創建日期,然后對其進行調整以找到按時間順序正確的歷史日期:

    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

這可能有效,但是您的問題不是很清楚。

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

這將檢查訂購日期是否在訂購年份的給定日期之間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM