2 Replies - 2468 Views - Last Post: 15 March 2011 - 12:29 AM Rate Topic: -----

#1 razree  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 01-March 11

TimeSpan-difference between two dates

Posted 14 March 2011 - 02:13 AM

Hi all
I want to be able to calculate the difference between two dates, written in SQL db. The difference must be in days/hours format. This is my code:

 Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()
        Dim dr As SqlDataReader
		
		Dim arrival As Date
        Dim departure As Date
		
        SQLConn.ConnectionString = ConnString
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = sqlstr
        dr = SQLCmd.ExecuteReader
         
		While dr.Read()
		arrival= dr.Item("ODHOD")
        departure = dr.Item("PRIHOD")
	    End While

        SQLConn.Close()


I've read timespan is one of the options to use for this case but don't know exactly how to do it..?
Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: TimeSpan-difference between two dates

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: TimeSpan-difference between two dates

Posted 14 March 2011 - 03:50 AM

Dim diff As TimeSpan = Date.Parse(dr.Item("PRIHOD")).Subtract(Date.Parse(dr.Item("PRIHOD")))


Then check out the properties, and the ToString method.
Was This Post Helpful? 1
  • +
  • -

#3 razree  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 01-March 11

Re: TimeSpan-difference between two dates

Posted 15 March 2011 - 12:29 AM

Hi
I decided it's probably easyer for me to work a way arroun. I have calculated dates from datetimepickers on another form, write diff to base and on another form just got this data out.
I used this code:

 Dim odhod As Date = DateTimePicker1.Value

        Dim prihod As Date = DateTimePicker2.Value
        Dim razlika As TimeSpan
        razlika = prihod.Subtract(odhod)

        'MessageBox.Show(razlika.Days.ToString() & "/" & razlika.Hours.ToString())


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1