Issue with Counter and a Date

XK8ER

I am havving a little issue with a date like the 1AM bellow but if I use a date like this it works fine "MAR 28, 2014 3PM" for some reason I get a "-17:49" while using the 1AM.. why is this?

Dim myTime As String = "MAR 26, 2014 1AM"
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
Dim date2 As DateTime = Convert.ToDateTime(myTime)
Dim ts As New TimeSpan
ts = date2 - date1

If ts.Days > 0 Then
    TextBox.Text = ts.Days & ":" & ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
ElseIf ts.Hours > 0 Then
    TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
Else
    TextBox.Text = ts.Minutes & ":" & ts.Seconds

    If ts.Minutes = 2 And ts.Seconds < 30 Then
        doStop = True 'we are 2:30 minutes away from closing
    End If

    If ts.Minutes < 15 Then
                'TextBox.text = "Ending Soon"
    End If
End If   
mellamokb

You are converting the current date/time to string, then auto-converting it back to a date value immediately on that second line.

Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
                                            ^^^^^^^^ this converts date to string
             ^^^^^^^^ this converts it back due to implicit casting

But since you are using hh as the format, you get only the 12-hour value of the date, so some information about the date value is lost.

For example, it is currently 12:34 AM EST where I am testing the code, but if I dump out the contents of date1 immediately after that second line, it has already become 12 hours later!

3/26/2014 12:34:41 PM

When you want to work with date values and do math on them, leave them as date values. Don't convert to string until you're ready to display something to the user. So just use:

Dim date1 as DateTime = System.DateTime.Now

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related