System.DateTime.Now.ToShortDateString

munkee

Active member
Joined
Feb 20, 2007
Messages
43
Programming Experience
Beginner
I tried to do a search and haven't really come up with anything so I figured I'll make this my first post.

Here is what I'm trying to do:

VB.NET:
Private Sub Form1_Load

Label2.Text = System.DateTime.Now.ToShortDateString

End Sub

Private Sub Button1_Click

TextBox1.Text = Label1.Text

End Sub


Private Sub  Timer1_Tick

If Label1.Text = Label2.Text Then
   MsgBox ("working")
End If

End Sub
Now, I figured this would have worked...but really, what is the problem here?... aside from my quick example code ;) - haha. Can you not have LabelText be equal to one another when using the DateTime?
 
Hi,

It is possible to compare the strings but I see that you are mixing up some Labels in your form. It will be just a simple string comparison.

You initialize the Label2 with the datetime. What value has Label1? There is no value in it according to the above code.

Greetz,

Geert
 
sorry - i don't know what i was thinking earlier. haha - that is seriously such a bad question! anyway - here is what i used.

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label2.Text = TextBox1.Text
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = System.DateTime.Now.ToShortTimeString
        If Label2.Text = Label1.Text Then
            MsgBox("working")
        End If
    End Sub
End Class
 
The value for TextBox1.Text is the time that I want it to activate the MsgBox.


In the actual application I have it using:

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = System.DateTime.Now.ToShortTimeString
        If Label2.Text = Label1.Text Then
            System.Diagnostics.Process.Start("Shutdown", "/s")
        End If
    End Sub

What I'm using this for is to shut down the system(s) at a given time. The application is ran at start up and the systems already have automatic start up built into the BIOS.
 
Ug.. Dont do that (string comparison and finger crossing).. instead do this:


VB.NET:
Class ..
 
  Private shutDownTime as DateTime = new DateTime(year, month, day, hour, minute, second)
 
  Timer1_Tick
    If DateTime.Now > shutDownTime Then ShutDown()
 
Ug.. Dont do that (string comparison and finger crossing).. instead do this:


VB.NET:
Class ..
 
  Private shutDownTime as DateTime = new DateTime(year, month, day, hour, minute, second)
 
  Timer1_Tick
    If DateTime.Now > shutDownTime Then ShutDown()


The way I went about it works fine, could you please give me a scenario where it would mess up? haha
 
cjard is right that it's better practice not to use string comparison with dates.

Suppose your program runs on an other pc with its international settings set otherwise than yours, for example his date will be represented as "13/12/2006" where yours is "12/13/2006".
String comparison could lead to unwanted behaviour, cjard's solution will always work.

I'm not saying your program will fail though, as i don't know how label2.text is filled.
 
Ok, that makes complete sense now. :)

I wasn't thinking his response was a wrong one, I just wanted to know the reasoning behind it! Knowledge is power!!!

I'll switch out the code I'm using for what he has suggested, after all - it's good practice. Luckily for the system(s) this application will be running on, will not leave the US.

The responses are greatly appreciated.


Thanks guys!
 
there's another thing with direct comparisons that is problematic

if youre checking for one thing equal another then it is conceivable that something might interrupt the process for long enough that the milestone is skipped

suppose you check every second whether the time = another time (to the exact second) of 12:34:56
at 12:34:55.99999 seconds, a check occurs
something the machine does (load a cdrom is a good one; that freezes everything for a moment) means the next timer tick occurs at time
12:34:57.00001 - yep, just 1.00002 seconds have passed because the machine was busy, but the string comparisons will fail!

note how i query whether the world time is ANY TIME AFTER the shutdown time..
i.e. the machine should have shut down by now...

its defensive coding..

Also, vb programmers have a bit of a habit of being sloppy with their datatypes and their thinking regards turning everything into a string and back all the time.. try to avoid that, work with things in their native type for as long as possible.. in this case, using strings is 100% unnecessary.. :)



while this case is nowhere near as bad as the following example, I really have seen this code:

Dim x as Integer = "1"
While(x < "10")
x = x + "1"
...

Its stuff like that that makes me cry.. "cjard for type strictness" ;)
 
You know, I have noticed that the time will be off if there is a process taking up the load of the computer. It hasn't really become a major concern until now; thank you for clearing this up with me.

I'm just getting back into the swing of things now. I started working with VB3 back in the early years (of VB) and then switched to web development. It's very exciting.

Again, thank you!
 
Back
Top