Home All Groups Group Topic Archive Search About
Author
10 Oct 2005 11:53 AM
Detlef Jockheck
Hi,

I have here some date strings like "01jan05", "10oct05" which should
convertet to "01.01.05", 10.10.05" etc. I'm not really familar with vb.
What is the easiest way to do this conversion in vb (with
example-code? :-))

ciao
Detlef

--
Detlef Jockheck

Author
10 Oct 2005 12:23 PM
Steven Burn
Debug.Print "Is a valid date: " & IsDate("01jan05")
Debug.Print "Formatted: " & FormatDateTime("01jan05", vbShortDate)

--
Regards

Steven Burn
Ur I.T. Mate Group
www.it-mate.co.uk

Keeping it FREE!

Show quoteHide quote
"Detlef Jockheck" <djockheck.hates_spam@gmx.de> wrote in message
news:pa1q13-5to.ln1@java.adp-entwicklung.gauselmann.de...
> Hi,
>
> I have here some date strings like "01jan05", "10oct05" which should
> convertet to "01.01.05", 10.10.05" etc. I'm not really familar with vb.
> What is the easiest way to do this conversion in vb (with
> example-code? :-))
>
> ciao
> Detlef
>
> --
> Detlef Jockheck
>
Author
10 Oct 2005 12:26 PM
Dave
Hi

CDate is what you are looking for, there is a slight problem in that it
needs a seperator between the parts of the month but if you are 100% sure
that your dates will always be in the format: "ddmmmyy" then the following
will convert the date string into a VB date variable:

Dim String2Date as Date
Dim strDate as String

String2Date = CDate(left$(strDate, 2) & " " & mid$(strDate, 3, 3) & " " &
right$(strDate, 2))

You can now use the Format function to display the date in any manner you
choose, look at the documentation for details.

Best Regards
Dave O.

Show quoteHide quote
"Detlef Jockheck" <djockheck.hates_spam@gmx.de> wrote in message
news:pa1q13-5to.ln1@java.adp-entwicklung.gauselmann.de...
> Hi,
>
> I have here some date strings like "01jan05", "10oct05" which should
> convertet to "01.01.05", 10.10.05" etc. I'm not really familar with vb.
> What is the easiest way to do this conversion in vb (with
> example-code? :-))
>
> ciao
> Detlef
>
> --
> Detlef Jockheck
>
Author
11 Oct 2005 10:48 AM
Detlef Jockheck
Dave wrote:

Show quoteHide quote
> Hi
>
> CDate is what you are looking for, there is a slight problem in that it
> needs a seperator between the parts of the month but if you are 100% sure
> that your dates will always be in the format: "ddmmmyy" then the following
> will convert the date string into a VB date variable:
>
> Dim String2Date as Date
> Dim strDate as String
>
> String2Date = CDate(left$(strDate, 2) & " " & mid$(strDate, 3, 3) & " " &
> right$(strDate, 2))
>
> You can now use the Format function to display the date in any manner you
> choose, look at the documentation for details.
>
> Best Regards
> Dave O.
>
> "Detlef Jockheck" <djockheck.hates_spam@gmx.de> wrote in message
> news:pa1q13-5to.ln1@java.adp-entwicklung.gauselmann.de...
>> Hi,
>>
>> I have here some date strings like "01jan05", "10oct05" which should
>> convertet to "01.01.05", 10.10.05" etc. I'm not really familar with vb.
>> What is the easiest way to do this conversion in vb (with
>> example-code? :-))
>>
>> ciao
>> Detlef
>>
>> --
>> Detlef Jockheck
>>
Hi Dave,

thanks :-) This works fine :-)))

ciao
Detlef

--
Detlef Jockheck
Author
10 Oct 2005 1:13 PM
Rick Rothstein [MVP - Visual Basic]
> I have here some date strings like "01jan05", "10oct05" which
should
> convertet to "01.01.05", 10.10.05" etc. I'm not really familar
with vb.
> What is the easiest way to do this conversion in vb (with
> example-code? :-))

First, let me say that you are terrible at picking examples to
show what you want.<g> Seriously, though, it is impossible to tell
from either of your examples what you want in the converted
dates... month.day.year ordering or day.month.year ordering. You
selected two examples in which the day and the month both have the
same numerical value!

Okay, now for your question. I assume that your given "dates"
always have leading zeroes for the month and day when either or
both are single digit values. So, the first thing you need to do
is add the separating spaces VB requires of its dates in the day
followed by 3-letter month followed by 2-digit date. This
statement will do that for you...

    Dim GivenDate As String
    Dim VBDate As Date
    GivenDate = "01Feb05"
    VBDate = CDate(Format(GivenDate, "@@ @@@ @@"))

At this point, the VBDate variable has the given date stored in a
variable declared as a Date type. Now, we can use the Format
function again to shape this date value into any other form we
want. Here are some examples.

    Print Format(VBDate, "dd.mm.yy")  ==>  01.02.05

    Print Format(VBDate, "mm/dd/yy")  ==>  01/02/05

and so on. You might want to look up the Format function in the
help files to see all of the different ways it can be used.

If you stick to simply **looking** at these values, everything
should be fine. HOWEVER, if you plan to use these converted date
value in any date-style manipulations (such as using the DateDiff
function for example), then you should be aware of the problems
your 2-digit year values might cause you. Here is something I have
posted in the past which should give you an idea of the possible
problems you could face in this situation...

Be careful here. The conversion VB performs when converting a text
string to a date uses the Regional Settings for the computer the
program is running on. The "breakpoint" between when 19 or 20 is
pre-pended to the 2-digit year is configurable by the user and can
vary from computer to computer (user to user). Start up the
Control Panel and select the Regional and Language Options app.
When it is running, click on the Regional Options tab (if it isn't
already selected), then click the Customize button, then click the
Date tab on the new dialog box that appears. Now look at the first
frame on that panel (labeled "Calendar"). From the Up/Down control
on the right you can change the date range for the 2-digit year.
Note what yours are set to (I'm assuming it is the original
default value or 1930 to 2029)... my computer is set for 2000 to
2099. That means if you enter a date like 8/4/99 into your
program, your system will expand it to 8/4/1999; however, if I
entered that same date into your program on my computer, it would
be expanded to 2099. You should really use 4-digit years.

Rick
Author
11 Oct 2005 8:30 AM
J French
On Mon, 10 Oct 2005 09:13:05 -0400, "Rick Rothstein [MVP - Visual
Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote:

<snip>

> That means if you enter a date like 8/4/99 into your
>program, your system will expand it to 8/4/1999; however, if I
>entered that same date into your program on my computer, it would
>be expanded to 2099. You should really use 4-digit years.

I've a suspicion that he is getting the dates from a file

To the OP

Break down the date and manually convert it, Mid$(), Left$(), Right$()

- check every bit carefully and report problems

Get the date into a Date Variable, ideally using DateSerial

It is a lot more work ( 10 mins max ) but VB is over easy when it
comes to converting things into dates
- so it will save you many curious problems in the long term