Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to Date conversion in GREEK

Status
Not open for further replies.

Takis

Instructor
Oct 12, 2001
57
0
0
GR
The following part of code tries to convert a string that looks like "290205" to date
Dim rcdatcom As New Date
...
rcyy = input.Substring(9, 2)
rcmm = input.Substring(11, 2)
rcdd = input.Substring(13, 2)
rcdate = Trim(rcdd + rcmm + rcyy)
rcdatcom = CDate(rcdate)

But every time an error occurs saying "Cast from string "022805" to type 'Date' is not valid."

Can anyone help please?
 
Code:
 rcdate = Trim(rcdd + rcmm + rcyy)
Isn't this just putting it back to what it was?

For the fewest problems in converting dates-as-strings to a DateTime, use yyyy-mm-dd as your format. This is the ISO-8601 standard and is recognized by almost everyone.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
??? ???? ????????????? ????:

Dim x As New Date(year, month, day)
MsgBox(x)


// ?? ?? ?????? CInt(Substring(..., 2)) ?????? ?? ???????? ???? Date()
 
Or you could try:

Dim s As String = "abcdefgh280205asfasfa"
Dim d As Date = CDate(s.Substring(8, 6).Insert(2, "/").Insert(5, "/"))
MessageBox.Show(Format(d, "dd/MMM/yyyy"))


Hope this helps.
 
The only it's going to work is to stick it in a form your computer will understand. So, to take ThatRickGuy's example...

Code:
rcyy = input.Substring(9, 2)
rcmm = input.Substring(11, 2)
rcdd = input.Substring(13, 2)
rcdate = Trim(rcmm + "/" + rcdd + "/" + rcyy)
rcdatcom = CDate(rcdate)

once you have an actual date data type, you can format it anyway you wany

Code:
messagebox.show(format(rcdatcom, "dd/MMM/yyyy"))

Dale
 
AccordingToDale, Takis hard coded the postion at which the date appeared in the source string, ThatRickGuy used the same positions in his example.

For my example I created a dummy string:

Dim s As String = "abcdefgh280205asfasfa"

Used the calculated positions of the date in that string (9 for the day, 11 for the month and 13 for the year and rather than create separate strings - in one expression:

inserted the "/" symbols in their correct positions
pulled out the required substring
converted the result to a date

Dim d As Date = CDate(s.Substring(8, 6).Insert(2, "/").Insert(5, "/"))


Finally, I proved the result with a MessageBox

By the way, my code example was copied from a working program - that at least my computer understood. So I don't understand what your complaint is???

Hope this helps.


 
Sorry earthandfire, no complaint.

Just some stuff I've encountered when working on machines in various environments is you gotta watch out for the regional settings.

When I ran your code on my machine it wouldn't work. If I played around the values, it would work fine, but as is, it would not cast to the date.

Anywhoo...

Dale
 
rcyy = input.Substring(9, 2)
rcmm = input.Substring(11, 2)
rcdd = input.Substring(13, 2)
Dim MyDate As Date = New Date(Cint("20"& rcyy.ToString()), rcmm, rcdd)
 
AccordingToDale, thats ok.

The code I posted was more or less taken from a routine that processes a fixed width file. You make a good point about Regional Settings.

In any event, however, my code was completely wrong [blush]. The input source format is YYMMDD not DDMMYY which I was processing.

Sorry all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top