FAST date string conversion ?

P

pamelafluente

A quick question,

I have a string with a date in european format, e.g. "30/4/2006".

What is the FASTEST way to produce the corresponding string in USA
format
"4/30/2006" and asian format "2006/4/30" ?

-pam
 
J

Jay B. Harlow [MVP - Outlook]

Pam,
Define "fastest", I would be more concerned with the "proper" way of doing
it.

If I had a date in d/m/y format & wanted m/d/y or y/m/d formats I would
simply use DateTime.Parse & DateTime.ToString. As Parse & ToString are the
"proper" ways of doing (who better then the type itself to have convert to &
from various formats).

Something like:

Const dmy As String = "d/M/yyyy"
Const mdy As String = "M/d/yyyy"
Const ymd As String = "yyyy/M/d"

Dim european As String = "30/4/2006"

Dim someDate As Date = DateTime.ParseExact(european, dmy, Nothing)

Dim usa As String = someDate.ToString(mdy)
Dim asian As String = someDate.ToString(ymd)

NOTE: Convert.ToString & Convert.ToDateTime ultimately call DateTime.Parse &
DateTime.ToString...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|A quick question,
|
| I have a string with a date in european format, e.g. "30/4/2006".
|
| What is the FASTEST way to produce the corresponding string in USA
| format
| "4/30/2006" and asian format "2006/4/30" ?
|
| -pam
|
 
P

pamelafluente

Thank you Jay, it helps a lot !

I have a doubt, why for the year there are 4 yyyy and for day/month on
one
(intuitively one would expect 2 chars ).

Thanks again,

-pam

PS by "fast" I meant the most efficient way in terms of speed of
execution. Probably the one you suggest :)
 
J

Jay B. Harlow [MVP - Outlook]

I used one on day & month so as to allow the 1st thru the 9th. without
requiring a leading zero.

It allows "30/4/2006" rather then require "30/04/2006"


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Thank you Jay, it helps a lot !
|
| I have a doubt, why for the year there are 4 yyyy and for day/month on
| one
| (intuitively one would expect 2 chars ).
|
| Thanks again,
|
| -pam
|
| PS by "fast" I meant the most efficient way in terms of speed of
| execution. Probably the one you suggest :)
|
 
P

pamelafluente

Very good. Thank you very much!

I guess that with the same logic it could be safe to use only 2 chars
for the year (yy) ...

-pam
 
C

Cor Ligthert [MVP]

Jay,

Just for you as American,
It allows "30/4/2006" rather then require "30/04/2006"

The second is AFAIK more common in Europe, that as well for the days.

Cor

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Thank you Jay, it helps a lot !
|
| I have a doubt, why for the year there are 4 yyyy and for day/month on
| one
| (intuitively one would expect 2 chars ).
|
| Thanks again,
|
| -pam
|
| PS by "fast" I meant the most efficient way in terms of speed of
| execution. Probably the one you suggest :)
|
 

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