Date Time problem

I

id10t error

I am makeing a program that will track productivity by every time a
button is pushed i write a record to a text file. The text file
contain the time in HHMMSS format. The next time they press a button i
want to read the last time and find the span of time between the two
buttons being pushed. Since I am writing the first time to a text file
i have to pull it back as a string. This would not be a problem until
we get to around midnight and i get a negative number. How can i
convert a string to HHMMSS?
 
H

Harry

id10t error said:
I am makeing a program that will track productivity by every time a
button is pushed i write a record to a text file. The text file
contain the time in HHMMSS format. The next time they press a button i
want to read the last time and find the span of time between the two
buttons being pushed. Since I am writing the first time to a text file
i have to pull it back as a string. This would not be a problem until
we get to around midnight and i get a negative number. How can i
convert a string to HHMMSS?

I thing, for a start, you need as method to prepend the date to your time
string. To the best of my knowledge, both DateTime.TryParse methods require
a date component. Perhaps you could try something like:

Function Build_Date(sTime as string) As Date

Dim tmp as string = Today.ToShortDateString

temp &= " " & sTime.Substring(0, 2) & ":"

temp &= sTime.Substring(2, 2) & ":"

temp &= sTime.Substring(4, 2)

Return CDate(tmp)

End Function

Better still, write the full date and time to your file in the first place.
eg MyDate.ToString("ddMMyyyyhhmmss")
 
I

id10t error

I thing, for a start, you need as method to prepend the date to your time
string. To the best of my knowledge, both DateTime.TryParse methods require
a date component. Perhaps you could try something like:

Function Build_Date(sTime as string) As Date

Dim tmp as string = Today.ToShortDateString

temp &= " " & sTime.Substring(0, 2) & ":"

temp &= sTime.Substring(2, 2) & ":"

temp &= sTime.Substring(4, 2)

Return CDate(tmp)

End Function

Better still, write the full date and time to your file in the first place.
eg MyDate.ToString("ddMMyyyyhhmmss")

I do have the full date and time. I could put them together. Would
that help me out?
 
P

Phill W.

id10t said:
I am makeing a program that will track productivity by every time a
button is pushed i write a record to a text file. The text file
contain the time in HHMMSS format.

Why? That's not a date format that Visual Basic can make sense of, so
why not save yourself a headache or two and use one that it can use?

DateTime.Now.ToString( "yyyy/MM/dd hh:mm:ss" )

will parse back into a DateTime variable very nicely, thank you.
The next time they press a button i want to read the last time and
find the span of time between the two buttons being pushed.

Far better to store this value in the program and calculate the elapsed
time based on that, rather than re-reading the file (which could get
very big and hence very slow to read).

Class Timings
Public Shared gLastActionTime as DateTime = DateTime.MinValue

Public Shared Sub TimedAction(sActionName as String)
Dim dtNow as DateTime = DateTime.Now
Dim lElapsed as Integer = 0

If gLastActionTime <> DateTime.MinValue Then
lElapsed = dtNow.Subtract( gLastActionTime ).TotalMilliseconds
End If
gLastActionTime = dtNow

WriteToFile( "{0:yyyyMMdd hh:mm:ss}|{1}|{2}" _
, dtNow, lElapsed, sActionName )

End Sub

HTH,
Phill W.
 
I

id10t error

Why?  That's not a date format that Visual Basic can make sense of, so
why not save yourself a headache or two and use one that it can use?

DateTime.Now.ToString( "yyyy/MM/dd hh:mm:ss" )

will parse back into a DateTime variable very nicely, thank you.


Far better to store this value in the program and calculate the elapsed
time based on that, rather than re-reading the file (which could get
very big and hence very slow to read).

Class Timings
    Public Shared gLastActionTime as DateTime = DateTime.MinValue

    Public Shared Sub TimedAction(sActionName as String)
       Dim dtNow as DateTime = DateTime.Now
       Dim lElapsed as Integer = 0

       If gLastActionTime <> DateTime.MinValue Then
          lElapsed = dtNow.Subtract( gLastActionTime ).TotalMilliseconds
       End If
       gLastActionTime = dtNow

       WriteToFile( "{0:yyyyMMdd hh:mm:ss}|{1}|{2}" _
          , dtNow, lElapsed, sActionName )

    End Sub

HTH,
    Phill  W.

Phill

That seemed to work out great. I like the format better than what i
was using. I thank you for your time and help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top