-4

I am facing a problem taking the difference between two times. I save a string variable, timeofday, into the database when the user closes the main window. I need to take the current time and find the difference between logged in and logged out time and save it as total time spent.

My problem is that I am not able to get the solution for converting string into time and finding the difference. Any suggestions?

djv
  • 15,168
  • 7
  • 48
  • 72
Kiran Rai Chamling
  • 460
  • 1
  • 6
  • 15
  • What does your "time" data look like in your database? – vintastic Nov 29 '16 at 19:12
  • If your db time is a string, select the string and use [DateTime.ParseExact](https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2) to convert it into a DateTime object...then you can do date/time math on it – soohoonigan Nov 29 '16 at 19:14
  • Its in HH:MM:SS 24hrs format – Kiran Rai Chamling Nov 29 '16 at 19:14
  • DateTime are easily saved as Date in nearly all databases. Time can be different because the type - TimeSpan - is less common and hard to parse. However, if you are `taking a difference between two times` save it as a DateTime. – Ňɏssa Pøngjǣrdenlarp Nov 29 '16 at 19:17
  • I just have a time in the above format. So, will it work? I was not able to work with the solution provided. – Kiran Rai Chamling Nov 29 '16 at 19:24
  • Welcome to StackOverflow! Your question is ambiguous because you've supplied very little information. Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) from the help center. Then ask a new question if necessary, using what you learned from these help articles to guide you. – Matt Johnson-Pint Nov 29 '16 at 19:25
  • The format is invalid. "HH" is correct, but "MM" corresponds to month, and "SS" doesn't correspond to anything. You probably want to use "HH:mm:ss" – djv Nov 29 '16 at 19:26
  • I am not working with date time so, for my convenience I kept it as string. But, here I am facing a problem. Is there any simple way to convert the time string (hh:mm:ss AM/PM)into time and take a difference? – Kiran Rai Chamling Nov 29 '16 at 19:27
  • Verdolino, that's correct,. So do you have any suggestion for this issue? – Kiran Rai Chamling Nov 29 '16 at 19:29
  • 1
    `for my convenience I kept it as string` once you do that you do not have a Date Time Issue, you have a *strings are not datetimes issue* – Ňɏssa Pøngjǣrdenlarp Nov 29 '16 at 19:57

1 Answers1

1

You should not save the datetime in the database as string. But if you must, here is how you can convert datetime to and from string. Find the duration on a timespan

Sub Main()

    ' generate two datetimes two seconds apart
    Dim d1 As DateTime = DateTime.Now
    Threading.Thread.Sleep(2000)
    Dim d2 As DateTime = DateTime.Now
    Console.WriteLine("d1: {0}, d2: {1}", d1, d2)

    ' convert the datetimes to strings and assign them
    Dim s1 As String = d1.ToString("HH:mm:ss")
    Dim s2 As String = d2.ToString("HH:mm:ss")
    Console.WriteLine("s1: {0}, s2: {1}", s1, s2)

    ' parse the strings back into datetimes
    d1 = DateTime.Parse(s1)
    d2 = DateTime.Parse(s2)
    Console.WriteLine("d1: {0}, d2: {1}", d1, d2)

    ' find the duration in total hours, minutes, or seconds
    ' the operation is performed on a timespan resulting from `DateTime.Subtract`
    Dim hours = d2.Subtract(d1).TotalHours
    Dim minutes = d2.Subtract(d1).TotalMinutes
    Dim seconds = d2.Subtract(d1).TotalSeconds

    ' note: use one of the following, as they represent the same duration
    ' not a composite time
    Console.WriteLine("Duration in hours: {0}", hours)
    Console.WriteLine("Duration in minutes: {0}", minutes)
    Console.WriteLine("Duration in seconds: {0}", seconds)
    Console.ReadLine()

End Sub

Output

d1: 11/29/2016 2:35:55 PM, d2: 11/29/2016 2:35:57 PM
s1: 14:35:55, s2: 14:35:57
d1: 11/29/2016 2:35:55 PM, d2: 11/29/2016 2:35:57 PM
Duration in hours: 0.000555555555555556
Duration in minutes: 0.0333333333333333
Duration in seconds: 2

Note: if you use that format, you will throw away the date component. The system will assume today and you may run into issues if the application is run overnight. Personally, I would supply "yyyy-MM-dd HH:mm:ss", or no format at all (the default format).

djv
  • 15,168
  • 7
  • 48
  • 72
  • 1
    Just to note - if you really are trying to measure elapsed time of an operation, `System.Diagnostics.Stopwatch` is much more efficient than subtracting two `DateTime` values. Also, if you are subtracting two *local* `DateTime` values, you might get caught by daylight saving time transitions. See http://codeofmatt.com/2015/03/06/common-daylight-saving-time-mistakes-for-net-developers/ – Matt Johnson-Pint Nov 29 '16 at 20:05
  • 1
    @MattJohnson Ok but I was only trying to answer the OP's question `My problem is that I am not able to get the solution for converting string into time and finding the difference.` as he's saving a string in a database, so he has other problems past this. I think more value would be realized in directing your *awesome* commentary [here](http://stackoverflow.com/a/2532962/832052) toward the OP. :) – djv Nov 29 '16 at 20:13