8 Replies - 1312 Views - Last Post: 31 July 2012 - 10:22 PM Rate Topic: -----

#1 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

adding the difference of days between two dates

Posted 31 July 2012 - 04:57 AM

i used this code to get the number of days between dates, :

Dim duedate As Date = dtpBorrow.Value 
                Dim tsTimeSpan As TimeSpan
                Dim noOfDays As Integer
                tsTimeSpan = Now.Subtract(duedate)
                noOfDays = tsTimeSpan.Days
                txtFine.Text = (noOfDays.ToString())


Now is my EndDate.
duedate is my startdate.

basically this code works fine, and gets the number of days between dates but it depends on the dgv row. what i mean is that my dtpBorrow changes depending on the row that i click. what i want to happen is to have another textbox that will contains the total number of days.
for example:
still considering that now is my endDate:
dgvRow 1 puts a date of 7/25/12 into dtpBorrow : 7/25/12 - 7/31/12 = 6 (6 is achieved through the code)
dgvRow 2 puts a date pf 7/26/12 into dtpBorrow : 7/26/12 - 7/31/12 = 5 (5 is achieved through the code)

so all in all i have 11. how can i achieve it through codes?

Is This A Good Question/Topic? 0
  • +

Replies To: adding the difference of days between two dates

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: adding the difference of days between two dates

Posted 31 July 2012 - 05:34 AM

instead of setting noOfDays equal to tsTimeSpan.Days, add to it.

Basically Initialize noOfDays to 0 then where you have
noOfDays = tsTimeSpan.Days



change it to
noOfDays += tsTimeSpan.Days


This post has been edited by rgfirefly24: 31 July 2012 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: adding the difference of days between two dates

Posted 31 July 2012 - 05:54 AM

i've tried you're suggestion of changing this
  noOfDays = tsTimeSpan.Days 
to
 noOfDays += tsTimeSpan.Days
but i still get the came up with the same result.
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: adding the difference of days between two dates

Posted 31 July 2012 - 05:58 AM

I suspect that you are resetting noOfDays back to zero every time your click event is firing. I would suggest moving the Dim for noOfDays to the class level, so that it persists it's value beyond just the single click event.

This post has been edited by rgfirefly24: 31 July 2012 - 06:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: adding the difference of days between two dates

Posted 31 July 2012 - 06:32 AM

still nothing happened. is it possible to convert dtpBorrow into number of days between dates and then just call it in the datagridview??
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: adding the difference of days between two dates

Posted 31 July 2012 - 08:19 AM

Show me the entirety of the code and I can take a look at it. What I advised you of should work unless this is a web app in which case you will have to either use a Session variable, or get the value of the textbox and add to it.
Was This Post Helpful? 0
  • +
  • -

#7 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: adding the difference of days between two dates

Posted 31 July 2012 - 07:48 PM

this is the whole code that i'm using,

Private Sub dgvIssuedBooks2_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvIssuedBooks2.CellClick
        If dgvIssuedBooks2.Visible = True Then
            con.Open()
            com.CommandText = "Select DueDate from IssueBooks where AccessionNumber = '" & dgvIssuedBooks2.CurrentCell.Value & "' and STATUS = 'BORROWED';"
            com.Connection = con
            c = com.ExecuteReader
            Try
                If c.Read = True Then
                    Me.dtpBorrow.Text = c.Item("DueDate").ToString
                End If
                Dim duedate As Date = dtpBorrow.Value
                Dim tsTimeSpan As TimeSpan
                tsTimeSpan = Now.Subtract(duedate)

                'Dim noOfDays As Integer
                'noOfDays = tsTimeSpan.Days
                'txtFine.Text = (noOfDays.ToString() * 2) & ".00"


                'Dim noOfDays2 As Integer 'I've put it in the class
                noOfDays2 += tsTimeSpan.Days
                txtFines.Text = noOfDays2.ToString() & ".00"

            Catch ex As Exception
            End Try
            con.Close()
        End If
    End Sub



maybe what you're suggesting is working and the problem might lies on my code..
Was This Post Helpful? 0
  • +
  • -

#8 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: adding the difference of days between two dates

Posted 31 July 2012 - 08:33 PM

just to asked if my idea is possible. can be textbox1 input be but in arrays? for example, i've input 1 and 1 will put in array(0), then i've input 2 and 2 will be put in array(1) and so on. and then i'll just add all the arrays to get the sum.. is that possible?
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: adding the difference of days between two dates

Posted 31 July 2012 - 10:22 PM

First know that those code are in cellClick event, which means you will have them executed when clicking the cell. Also since now you have declared noOfDays2 as class variable, it should work. Is it?

Quote

just to asked if my idea is possible. can be textbox1 input be but in arrays? for example, i've input 1 and 1 will put in array(0), then i've input 2 and 2 will be put in array(1) and so on
You can use 2D array for that,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1