|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
convert month to monthHi,
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 Debug.Print "Is a valid date: " & IsDate("01jan05")
Debug.Print "Formatted: " & FormatDateTime("01jan05", vbShortDate) 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 > 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 > Dave wrote:
Show quoteHide quote > Hi thanks :-) This works fine :-)))> > 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, ciao Detlef -- Detlef Jockheck > I have here some date strings like "01jan05", "10oct05" which First, let me say that you are terrible at picking examples toshould > 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? :-)) 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 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 I've a suspicion that he is getting the dates from a file>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. 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
I must be blind -- what's wrong with this sub?
.NOT My Views Reverse Array order ? Cannot Load Form! How should one form call another? RESET does what? Why can't I test class functions from the Immediate window? advanced tooltip bubbles question Activex Exe Point me in the right direction - Mouse utility... |
|||||||||||||||||||||||