Adding time to date, then adding another time

Calculating total usage based on times used

Page 1 of 1

3 Replies - 8562 Views - Last Post: 08 November 2010 - 07:16 AM Rate Topic: -----

#1 MikeUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-November 10

Adding time to date, then adding another time

Posted 07 November 2010 - 11:45 AM

Hello, been racking my brains on this one for a long time, and I've finally decided to ask the question.

I had sevaral fields on a vb.net form which need to come together

Date = date the machinery was switched on
start Time = Time (on the above date) when the machinery was switched on
Stop time = Time when the machinery was switched off - BUT if stop time < start time then add one day to the Date because we have obviously gone past midnight.

Used Time = Number of hours:Minutes between start and stop.

Original total time = the Total Hours:minutes used life of the machinery (this I retrieve from another source)
New Total Time = The Original Total Time + Used Time

The original total time is over 15,000 hours, ie 15421:35

I keep finding answers, and applying them to a test program, but get plenty of errors like (Conversion from string "10004:11" to type 'Date' is not valid.)

Any help would be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Adding time to date, then adding another time

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Adding time to date, then adding another time

Posted 07 November 2010 - 12:15 PM

To add to a date, you can use .AddDays

Dim dt as Date = #11/7/2010#
dim newDate as Date = dt.adddays(1)


NewDate is now equal to 11/8/2010

You can also use TimeSpan to get the difference between two times.

as far as your number hours:min. You could split the string based on the : and perform the calculation against todays date.
      Dim originalTotalTime As String = "15421:35"
        Dim HrsMin() As String = originalTotalTime.Split(":"c)
        Dim hours As Integer = CInt(HrsMin(0))
        Dim minutes As Integer = CInt(HrsMin(1))


This would put 15421 into an integer and 35 into another integer variable

Repeat this for your current time used (based on the same input ie: 11:15) then you can add them together and save it for the next addition of time to the machine.

This post has been edited by CharlieMay: 07 November 2010 - 12:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 MikeUK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-November 10

Re: Adding time to date, then adding another time

Posted 08 November 2010 - 02:17 AM

View PostCharlieMay, on 07 November 2010 - 11:15 AM, said:

To add to a date, you can use .AddDays

Dim dt as Date = #11/7/2010#
dim newDate as Date = dt.adddays(1)


NewDate is now equal to 11/8/2010

You can also use TimeSpan to get the difference between two times.

as far as your number hours:min. You could split the string based on the : and perform the calculation against todays date.
      Dim originalTotalTime As String = "15421:35"
        Dim HrsMin() As String = originalTotalTime.Split(":"c)
        Dim hours As Integer = CInt(HrsMin(0))
        Dim minutes As Integer = CInt(HrsMin(1))


This would put 15421 into an integer and 35 into another integer variable

Repeat this for your current time used (based on the same input ie: 11:15) then you can add them together and save it for the next addition of time to the machine.


This was a huge help, thankyou, but the time 15421:35 is a calculated usage time, not a text entry, so when vb.net retrieves this value from the spreadsheet, it treats the number as a decimal = "500.363194444444"
Is there a way I can get vb.net to recognise the number as the time value 15421:35 and not the decimal equivalent,
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Adding time to date, then adding another time

Posted 08 November 2010 - 07:16 AM

Well, if I'm understanding correctly, 500.363194444444 would equal 8:20 am in excel with is 500.36 minutes from midnight. So if that is the case you should be able to use
        Dim StartTime As Date = #12:00:00 AM#
        Dim MyTimeFromExcel As Decimal = 500.363194444444
        Dim MyNewTime As Date = StartTime.AddMinutes(MyTimeFromExcel)

        Dim HrsMin() As String = MyNewTime.ToString("h:mm").Split(":"c)
        MessageBox.Show("Converted Time From Excel: " & MyNewTime & Environment.NewLine & _
                        "Hours from Converted Time: " & HrsMin(0) & Environment.NewLine & _
                        "Mins from Converted Time:  " & HrsMin(1))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1