-1

I'm running an application in which I'm able to obtain the required times, however when I try to deduct one value from another it comes up with error message stating conversion from date type cannot happen to timespan. The variables I use are shown below:

Dim downtime As TimeSpan
Dim uptime As TimeSpan 

Dim errortime As String() = lb_critical.SelectedItem.Split({" | "}, StringSplitOptions.RemoveEmptyEntries)
Dim errortimefound As DateTime = Convert.ToDateTime(errortime(0).ToString())
downtime = DateTime.Now - errortimefound
uptime = DateTime.Now - Convert.ToDateTime(downtime.ToString())

When I run this I get error with downtime variable.

To try and resolve, I tried converting the variables to all DateTime before deducted them, had a look on the internet but wasn't successful.

Could anyone identify what I am doing wrong here.

Thank you Andrew for improved code, but unfortunately I'm still getting the error. Please see the image below:

enter image description here

Satvir Singh
  • 61
  • 4
  • 16
  • why you want to convert time span to date time ? check this for more http://stackoverflow.com/questions/10276228/timespan-to-datetime-conversion – Abdul Jun 10 '16 at 11:42
  • I had a look at that site already tried the method but it didn't work. – Satvir Singh Jun 10 '16 at 12:10
  • 1
    `It is not very logical to convert TimeSpan to DateTime` Or vice versa. Subtracting one DateTime from another will not result in the first error message mentioned. We cant see that code, but it is doing something else to cause that error – Ňɏssa Pøngjǣrdenlarp Jun 10 '16 at 13:17
  • Please read my reply for Andrew, I have explained exactly what I am trying to do. – Satvir Singh Jun 10 '16 at 13:24

1 Answers1

0

Here is a sample of a working method:

Private Sub Button54_Click(sender As System.Object, e As System.EventArgs) Handles Button54.Click
    getTimes("wibblewoffle")
    getTimes("2016-06-10 08:55:12")
End Sub

Private Sub getTimes(foundTime As String)

    Dim errortimefound As DateTime

    If DateTime.TryParse(foundTime, errortimefound) Then

        Dim downtime As TimeSpan = DateTime.Now.Subtract(errortimefound)
        Dim uptime As TimeSpan = DateTime.Now.Subtract(Convert.ToDateTime(downtime.ToString()))

        'do your magic here

    Else
        MessageBox.Show(String.Format("This is not a valid date time: =>[{0}]", foundTime))
    End If

End Sub
Andrew Mortimer
  • 2,380
  • 7
  • 31
  • 33
  • Andrew please have a look at image in post. This error message occurred using the improved code you provided me with. This is something to do with the errortime, as your code works if the date is entered manually, but doesn't work if passed using errortime variable. – Satvir Singh Jun 10 '16 at 12:06
  • What exactly is in the string you're passing? I'm guessing it isn't understood as a valid datetime. You can try DateTime.TryParse to validate it. – Andrew Mortimer Jun 10 '16 at 12:18
  • I have an html file that I'm reading data from and I fill a listbox with the data. Then I ask the user to select one of the line from the list and when they do I want to get that line's Date and Time. The data looks like 21/04/2016 08:53:44 | 21/04/2016 08:53:44 | frmManagement.LiquidInThisPositionToolStripMenuItem_Click(): Exception occurred – Satvir Singh Jun 10 '16 at 13:00
  • I think downtime is correct. It is returning the timespan between when errortimefound and Now. What exactly are you looking for in with uptime? – Andrew Mortimer Jun 10 '16 at 14:46
  • yes you are right downtime works fine. There's a problem with uptime and system is not recognizing it as DateTime. The uptime is the value for how long a system is running for. As this problem will be used straight after the system finishes the tasks, I want the uptime to be uptime = now - starttime (I have this value already) - downtime. But it's not working with now -downtime currently. The main problem is converting from timespan to datetime and through research I found that if the timespan has a day it cannot convert to datetime. Is this true? – Satvir Singh Jun 13 '16 at 07:24
  • Wouldn't 'uptime' then just be 'starttime - error found time' ? – Andrew Mortimer Jun 13 '16 at 07:32
  • Let's go into detail. I have a database that needs startime, endtime, uptime, downtime. The startime is when process started and endtime when it stopped. I use errorfoundtime to have the time at which the error occurred. Downtime is used to detect when the system started again by taking the now substract the errortime. This gives the time when the system stopped and wasn't doing anything, because when this program is used to write values into the database, that is when the system begins. The uptime is thedifference between the next system starttime and previous system starttime. – Satvir Singh Jun 13 '16 at 07:54
  • Ok, so in your model, starttime and endtime should be dates. Uptime and downtime should be integer number of days / hours (or however you are measuring). Is that right? – Andrew Mortimer Jun 13 '16 at 08:36
  • yes that's right. In order to upload the data into the database they need to be in a specific data format which I can do If I'm able to get each of the individual component (day, month, hour, ...). I was able to get the hour, minute , second using uptime.Hours.ToString . However, I also need to get the day and month, but this gives weird values, not sure how to get it. I tried the following for getting the day component but no luck: Dim downtime As TimeSpan = TimeSpan.Parse("10/06/2016 08:55:12") MessageBox.Show(downtime.Days.ToString) – Satvir Singh Jun 13 '16 at 09:08
  • I think you're mixing up DateTime and TimeSpan. TimeSpan is the amount of time that has elapsed between two dates. – Andrew Mortimer Jun 13 '16 at 09:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114509/discussion-between-satvir-singh-and-andrew-mortimer). – Satvir Singh Jun 13 '16 at 09:20