formatting time and date

C

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.
 
L

Larry Lard

cj said:
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.
 
C

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 said:
cj said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top