Number of days

calculate number of days between two dates

Page 1 of 1

9 Replies - 7045 Views - Last Post: 14 May 2009 - 07:33 PM Rate Topic: -----

#1 yangma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-May 09

Number of days

Post icon  Posted 11 May 2009 - 09:54 PM

hi all...how can i calculate number of days between two dates.....for xample between 24/12/2009
Is This A Good Question/Topic? 0
  • +

Replies To: Number of days

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Number of days

Posted 11 May 2009 - 11:44 PM

Try DateDiff function.
http://www.w3schools...nc_datediff.asp
Was This Post Helpful? 0
  • +
  • -

#3 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

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

Was This Post Helpful? 0
  • +
  • -

#4 Poppa Mintin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 13-September 08

Re: Number of days

Posted 13 May 2009 - 10:45 AM

View Postyangma, 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.
Was This Post Helpful? 0
  • +
  • -

#5 yangma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-May 09

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
Was This Post Helpful? 0
  • +
  • -

#6 Lumenii  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 04-January 07

Re: Number of days

Posted 13 May 2009 - 09:53 PM

Well, using the following code (given above):

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

Was This Post Helpful? 0
  • +
  • -

#7 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

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!)
Was This Post Helpful? 0
  • +
  • -

#8 yangma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-May 09

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
Was This Post Helpful? 0
  • +
  • -

#9 Poppa Mintin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 13-September 08

Re: Number of days

Posted 14 May 2009 - 07:25 PM

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

  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.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1