hi all...how can i calculate number of days between two dates.....for xample between 24/12/2009

## 9 Replies - 11800 Views - Last Post: 14 May 2009 - 07:33 PM

##
**Replies To:** Number of days

### #2

## Re: Number of days

Posted 11 May 2009 - 11:44 PM

### #3

## Re: Number of days

Posted 12 May 2009 - 12:00 AM

Friend WithEvents Timer1 As Timer = New Windows.Forms.Timer Dim XmasEve As DateTime = New DateTime(Now.Year, 12, 24, 0, 0, 0, 0) Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click Select Case btnNext.Text Case "Start" Timer1.Interval = 100 Timer1.Start() Case "Stop" Timer1.Stop() End Select End Sub Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim Span As TimeSpan = XmasEve.Subtract(Now) txtOutput.Text = Span.Days & " Days, " & _ Span.Hours & " Hours, " & _ Span.Minutes & " Minutes, " & _ Span.Seconds & " Seconds, " & _ Span.Milliseconds & " Milliseconds until Xmas Eve" End Sub

### #4

## Re: Number of days

Posted 13 May 2009 - 10:45 AM

yangma, on 11 May, 2009 - 08:54 PM, said:

hi all...how can i calculate number of days between two dates.....for xample between 24/12/2009

Ok the easiest way to find the number of days between any two dates is to use: -

Difference = DateDiff(DateInterval.Day, Date1, Date2)

Where Difference = Integer: Date1 and Date2 = Date:

You will have to use the correct format for the two dates, this could depend on your machine’s local setting for time and date.

If Date1 is later than Date2 Difference will be a negative number.

Poppa.

### #5

## Re: Number of days

Posted 13 May 2009 - 09:45 PM

between march 1 and march 30...it results 29 days...it should have resulted 30 days...it subtracts 1 from 30 so that result is 29 days....how can i get 30 days from those two dates...want to include march 30 also..thanks

### #6

## Re: Number of days

Posted 13 May 2009 - 09:53 PM

Well, using the following code (given above):

If you just wanted to add one in order to include the last (or first) day, it would be:

However you may want to make sure that your difference is positive before adding that (because, as said above, if the first date is later than the second the result would be negative), so try:

Unless of course you would want a negative result if the first day was later than the second, in which case:

Remember that "Difference += 1" is shorthand for "Difference = Difference + 1".

That should do it for you. Not 100% sure but pretty close.

Difference = DateDiff(DateInterval.Day, Date1, Date2)

If you just wanted to add one in order to include the last (or first) day, it would be:

Difference = DateDiff(DateInterval.Day, Date1, Date2)

However you may want to make sure that your difference is positive before adding that (because, as said above, if the first date is later than the second the result would be negative), so try:

Difference = Math.Abs(DateDiff(DateInterval.Day, Date1, Date2)) + 1

Unless of course you would want a negative result if the first day was later than the second, in which case:

Difference = DateDiff(DateInterval.Day, Date1, Date2) If Difference < 0 then Difference -= 1 If Difference > 0 then Difference += 1

Remember that "Difference += 1" is shorthand for "Difference = Difference + 1".

That should do it for you. Not 100% sure but pretty close.

This post has been edited by **Lumenii**: 13 May 2009 - 09:56 PM

### #7

## Re: Number of days

Posted 13 May 2009 - 09:56 PM

you are changing what you asked for - the number of days between is 29, the number of days in the range is 30 - this is known as the fence post problem - a 30ft fence with posts every 10ft requires 4 posts - for 3 sections of fence (unless you connect the ends of the fence together - then 3 is correct)

|xxx|xxx|xxx|

simply add 1 to your data differential calculation to get what you want.

so you would define the difference between today and yesterday as 2 days - go for it (new math!)

|xxx|xxx|xxx|

simply add 1 to your data differential calculation to get what you want.

so you would define the difference between today and yesterday as 2 days - go for it (new math!)

### #8

## Re: Number of days

Posted 13 May 2009 - 10:18 PM

haha..m talking abt new math..m not doing thesis hahahaha....anyways thanks

m not talking abt new math....if i select march 30 then i wanted to display record of march 30 also...that's all i wanted not creating new math lol

m not talking abt new math....if i select march 30 then i wanted to display record of march 30 also...that's all i wanted not creating new math lol

### #9

## Re: Number of days

Posted 14 May 2009 - 07:25 PM

Yangma, Not wishing to write your program for you but… how’s about; -

Or: -

And then: -

Still bearing in mind which date comes first.

Poppa.

Date2 = Date2.AddDays(1)

Or: -

Date1 = Date1.AddDays(-1)

And then: -

Difference = DateDiff(DateInterval.Day, Date1, Date2)

Still bearing in mind which date comes first.

Poppa.

### #10

## Re: Number of days

Posted 14 May 2009 - 07:33 PM

Or could the .net approach and do.

Dim Date_1 As New Date(2009, 1, 1) Dim Date_2 As New Date(2009, 12, 31) Dim Date_Delta As TimeSpan = Date_2 - Date_1 'Date_Delta.Days is the number of days between them

This post has been edited by **AdamSpeight2008**: 14 May 2009 - 07:33 PM

Page 1 of 1