Home All Groups Group Topic Archive Search About

formatting time and date

Author
6 Mar 2006 5:00 PM
cj
Is this the .net way?

Dim msgDate As Date = Now
TextBox1.Text = Format(msgDate, "yyMMdd") 'I want year month day 060306
TextBox2.Text = Format(msgDate, "HHmmss") 'I want 13:23:58

I didn't think .net encourages functions like format(var, string) any
more but I'm not sure what takes it's place.

Author
6 Mar 2006 5:34 PM
Larry Lard
cj wrote:
> Is this the .net way?
>
> Dim msgDate As Date = Now
> TextBox1.Text = Format(msgDate, "yyMMdd") 'I want year month day 060306
> TextBox2.Text = Format(msgDate, "HHmmss") 'I want 13:23:58
>
> I didn't think .net encourages functions like format(var, string) any
> more but I'm not sure what takes it's place.

You might prefer

TextBox1.Text = msgDate.ToString("yyMMdd")
TextBox2.Text = msgDate.ToString("HH:mm:ss")

I suppose it could be argued that using the Date[Time]'s own ToString
is more 'the .net way'. "In general, if you want a string
representation of an object, ask its ToString to do the job" being the
principle at work.


--
Larry Lard
Replies to group please
Author
6 Mar 2006 7:08 PM
cj
Yes, that is what I was looking for.  I'd tried, well I thought I tried
that earlier but couldn't get it to work.  Wasn't sure what I should be
doing so I thought I'd ask.  Thanks Larry.

Oh, and I really don't prefer it but I'm trying to move forward with the
..net way of doing things.  :)  Lastly I see an error in my example.  I
wrote I wanted 13:23:58 but I really wanted 132358 so I'll just remove
the colons.

Larry Lard wrote:
Show quoteHide quote
> cj wrote:
>> Is this the .net way?
>>
>> Dim msgDate As Date = Now
>> TextBox1.Text = Format(msgDate, "yyMMdd") 'I want year month day 060306
>> TextBox2.Text = Format(msgDate, "HHmmss") 'I want 13:23:58
>>
>> I didn't think .net encourages functions like format(var, string) any
>> more but I'm not sure what takes it's place.
>
> You might prefer
>
> TextBox1.Text = msgDate.ToString("yyMMdd")
> TextBox2.Text = msgDate.ToString("HH:mm:ss")
>
> I suppose it could be argued that using the Date[Time]'s own ToString
> is more 'the .net way'. "In general, if you want a string
> representation of an object, ask its ToString to do the job" being the
> principle at work.
>
>