hi,
how do i retrieve the current date and put it into a string ?
thx
Printable View
hi,
how do i retrieve the current date and put it into a string ?
thx
VB Code:
Dim strDate as String = Date.Now.ToString()
This is a very basic question, and I would advise you to get a book on programming with vb.net. It would be much faster for you to learn then asking many questions (this is what usually happens). Most people are very willing to help, but a person should at least try to solve issues themselves. Its not just about getting something done, but how you got there that makes the difference between a coder and a script kiddie :).
I agree.
Hi Grimfort,
Is there any advantage between
Dim strDate As String = CStr(Date.Now)
and
Dim strDate as String = Date.Now.ToString() ??
With regards to this particular example, theres no reason to choose one above the other. Cstr returns a short date, and .ToString also does, but in other cases this is not always true. I spose its what I am used to doing.
Try a CStr on an enum and it returns the value, where as a ToString returns the actual enum name, for example.
A positive thing for a date.ToString, is that you can pass it the format you wish to use, or call other methods like .ToLongDateString or ToLongTimeString.
The CStr/Clng/CInt are called in-line so dont need to call a sub function to convert it, I would assume this makes it faster, but the documentation does not explicitly say this.
Hi Grimfort,
Many thanks.:wave:
actually, as basic as that question is, it really can be thought of as a good pointer to what the ToString() method does.
FYI, you can take practically any primitive data type or object and use ToString() to get the string representation of that value, name, etc. I say practically cause it doesn't work on all objects.
If you use Option Strict, ToString() can be your best friend if you have a lot of text to work with. :D
just my .02