1

I am trying to work out the number of days between two dates and whether it is a full month or not.

I currently have this code:

    Dim fromdate As Date
    Dim todate As Date
    Dim timespan
    Dim num_days As String
    Dim month As DateTime

    fromdate = Date.Parse("01/01/2017")
    todate = Date.Parse("31/01/2017")
    TimeSpan = todate - fromdate
    num_days = TimeSpan.Days

    MsgBox(num_days)

And then I try to work out if it is a full month:

month = todate
If (month.Month = "02" And num_days = "28") Or num_days = "29" Or num_days = "30" Or num_days = "31" Or num_days = "0" Then
    'do code here
Else
    'do code here
End If

But this is proving to not work in February because it only sees the num_days as 27

Is there a way I can get the correct number of days between the two dates including the dates themselves being full days?

And how can I check if it is a full, correct month or not

UPDATE

The purpose of this is for a billing system so files are read into a database with from and to dates then it needs to work out the pricing from those dates.

So a product has a specific price, but first of all we need to work out whether to bill for part of a month or a full month (basically part of the product price or the full price)

So these 'Full Month' date range examples will bill the full price

Full Month:

  • 01/01/2017 - 31/01/2017
  • 25/01/2017 - 25/01/2017
  • 18/01/2017 - 18/02/2017
  • 10/01/2017 - 10/01/2018

Whereas, this date range for 'Part Month' will only bill for the number of days between the from and to date (+1 day)

Part Month

  • 15/01/2017 - 31/01/2017
charlie
  • 415
  • 4
  • 35
  • 83
  • Did you notice the 'RELATED' section on the right? There are a lot of similar questions (if not a direct duplicate) None of them is good for you? – Steve Feb 04 '17 at 11:13
  • no i dont see that section? – charlie Feb 04 '17 at 11:17
  • I checked a few other questions but cannot find any the same as mine. I want to be able to find not only the number of days BETWEEN the 2 dates, but also include the dates. for example, if its 01/01/2017 to 04/01/2017 i should return 4 and not 3 – charlie Feb 04 '17 at 12:59
  • Alright, I deleted my answer for now – KekuSemau Feb 04 '17 at 13:46
  • Another attempt.. – KekuSemau Feb 04 '17 at 13:51
  • 1
    The problem with this question is... what is a month... A month is really a vague concept in general... Perhaps back it up a bit an try and explain what your are trying to achieve.. (not the code... but the desired functionality.) – Trevor_G Feb 04 '17 at 13:54
  • ok, take a look now – charlie Feb 04 '17 at 13:57
  • OK so can one guarantee that the month part of the date will ALWAYS be the "Next month".... Ie could it ever be like Jan 29th - Mar 2nd – Trevor_G Feb 04 '17 at 14:07
  • correct, it will always either be the same month or just the next month – charlie Feb 04 '17 at 14:09
  • there is another that i have just added too - 10/01/2017 - 10/01/2018 - could be the same date but just the next year – charlie Feb 04 '17 at 14:10
  • how about this if statement: `If days_between+1 = num_of_days_in_month Or fromdate = todate Or (fromdate.Day = todate.Day And todate.Month = fromdate.Month+1) Or (fromdate.Day = todate.Day And fromdate.Month = todate.Month And todate.Year = fromdate.Year+1) Then 'Full Month Else 'Part Month End If` – charlie Feb 04 '17 at 15:00

1 Answers1

2

This would get what you specified:

Dim IsOneFullMonth = (d1.Day = 1 And d2 = d1.AddMonths(1).AddDays(-1))
Dim IsOnMonthLater = (d2 = d1.AddMonths(1))

The two states are not really the same thing in my understanding.
The second check is one month and one day. (The first would match 'Jan 01 - Jan 31', the second one 'Jan 01 - Feb 01'.)

Also consider that neither check would match 'end of months' like 2016-02-29 - 2016-03-31 - you have to really define what you want to achieve in those cases.

KekuSemau
  • 6,830
  • 4
  • 24
  • 34
  • i was actually just trying this before you posted it :) that seems to be working, although ive run into something else. if the dates are 18/01/2017 - 18/02/2017 it is seeing that as 32 days and therefore thinks its part of a month and not a full month because 32 <> 31 (31 = days in the month) – charlie Feb 04 '17 at 13:29
  • I guess I need to do <= rather than = – charlie Feb 04 '17 at 13:30
  • what do you think? – charlie Feb 04 '17 at 13:37
  • I added something – KekuSemau Feb 04 '17 at 13:39
  • ok, see my update for what i mean by full and part month. sorry i should have explained a little more – charlie Feb 04 '17 at 13:43
  • i have just tried this and these dates are showing as part of a month: (18/01/2017 - 18/01/2017) but that should show as a Full Month and these are also showing as Part Month but they should be a full month: (18/01/2017 - 18/02/2017) – charlie Feb 04 '17 at 14:39
  • I have added this code `If fromdate.Day = 1 And todate = fromdate.AddMonths(1).AddDays(-1) Then 'full month Else 'Part Month End If` – charlie Feb 04 '17 at 14:40