views:

22

answers:

1

Hey everyone,

I am having a problem with Format not working. I have tried several standard formats but it doesn't effect the output at all.

Dim tempstring As String = Format(Now(), "M, m") and Dim date As Date = Format(Now(), "M, m")

both have a final value of "8/16/2010 10:52:21 AM"

Thanks for the help.

Update I am still having problems in my program. I have tried format in a test console app and it works fine, so the problem is not vb or vs.

+2  A: 
Dim date As Date = Format(Now(), "M, m")

This will never keep a format, as you're putting a string into a date value (I haven't tried it, but I'd be surprised if it didn't throw an error)

Dim tempstring As String = Format(Now(), "M, m")

This works a treat for me, giving "8, 2" (M-month, m-minute)

Update My full test code,

Dim tempstring As String = Format(Now(), "M, m")
Console.WriteLine(tempstring)

Console.WriteLine("Press any key")
Console.ReadKey()

gives (when run in august, and 2 minutes past the hour)

8, 2   
Press any key
Binary Worrier
I didn't think date as date would work, but tried it anyway. I can't get Dim tempstring As String = Format(Now(), "M, m")to work. Any ideas why?
Casey Haralson
@Casey Haralson: By "not work", what exactly do you mean? What result do you get? Also, can you put this part in a console app and get it to fail? I don't think the format is your problem, there's something going wrong somewhere else.
Binary Worrier
Ok, I tried it in a console app and format worked perfectly. In my regular app the output of the format is "8/16/2010 10:52:21 AM". Another thing I noticed is that putting the output of the format into a variable as date type does not throw an error.
Casey Haralson
@Casey: Dude, your problem isn't with the format, there's something else going wrong mate. You need to identify what that is.
Binary Worrier
Sure, I understand that. I have figured out a work around to my problem (.ToString(formating stuff here) worked for me). Thanks for the help.
Casey Haralson
Glad to help. Folks are more likely to respond to other questions you might ask if you have a high accept rate for your questions (hint: You could accept this as the correct answer, we both get extra rep for it. Just saying . . .)
Binary Worrier