Solved

CDate conversion error

Posted on 2003-12-06
12
1,565 Views
Last Modified: 2007-12-19
Hi

I have a textbox ticketDate.text which have value "21-10-2003".

I used the CDate function to conversion it :-  CDate(ticketDate.Text) but have
run time error.
   Cast from string "21-10-2003" to type "Date" is not valid.

Pls help

Thanks & Rgds
Heng03
0
Comment
Question by:heng03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 9887825
the error rise because the format is 2003-10-21
CDate uses standarts for datatimeformats
if you want to use your custom format you can use ticketDate.Text.ToString("dd-mm-yyyy");

B..G
0
 

Author Comment

by:heng03
ID: 9887890
Hi mmarinov

Your suggestion have syntax error.


Thanks & Rgds
Heng03
0
 

Author Comment

by:heng03
ID: 9887892
Hi mmarinov

Your suggestion have syntax error & compilation error.
It do not work.


Thanks & Rgds
Heng03
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 28

Assisted Solution

by:mmarinov
mmarinov earned 25 total points
ID: 9887919
sorry, i saw it too late
i gave the code to convert datetime object to string

so to be sure that you can convert string to datetime use this
DateTime.Parse(ticketDate.Text)

you can write it in
try
{
DateTime.Parse(ticketDate.Text)
}
catch
{
Response.write ( "You have entered wrong date! Please try again." )
}

B..G
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 25 total points
ID: 9889383
Hi heng03:
I had no problem

Created a textbox and changed its Text property to 21-10-2003

This is my code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(CStr(CDate(TextBox1.Text)))

Compiled and ran OK.

Dabas
0
 
LVL 1

Accepted Solution

by:
MarcGraff earned 25 total points
ID: 9890145
I'm sure this is the wrong way of doing this PLUS Dabas is far better at this than I and his way worked for me but

        Dim TmpStr() As String
        Dim AnsDate As Date
        TmpStr = Split(ticketDate.Text)
        AnsDate = CDate(TmpStr(1) & "/" & TmpStr(0) & "/" & TmpStr(2))
        MsgBox(AnsDate)

   - Marc
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9890199
use a datetimepicker control, it's easier and user friendly.
0
 

Author Comment

by:heng03
ID: 9890296
Hi
0
 
LVL 1

Expert Comment

by:MarcGraff
ID: 9890311
Sweet! Thanks!

   - Marc
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9890319
heng03:
Thanks for the points!
Just out of curiosity, how does your corrected code look like?

Dabas
0
 

Author Comment

by:heng03
ID: 9891767
Hi everyone

I used the sample code from MarcGraff, it is the best for my situation.

My code is below.

dim TmpStr() As String
            TmpStr = Split(txtTicketDate.Text, "-")
            Try
                      TicketDate = CDate(TmpStr(1) & "-" & TmpStr(0) & "-" & TmpStr(2))
            Catch
                      validYes = False
                                errMsg &= "Error Ticket Date : Shall be dd-mm-yyyy. Please  try
                                                 again." + vbNewLine
            End Try

The reason is that, it re-substring my date from dd/mm/yyyy to mm/dd/yyyy which is
suitable for the us-EN culture date-time format ("d").
As such, the CDate(expression) will work fine. else error if format is dd/mm/yyyy.

Thanks & Rgds
Heng03

0
 

Author Comment

by:heng03
ID: 9891772
Hi everyone,

I increased the points from 50 to 75.
And split among 3 of you. Each 25 points.

Sorry for that.

Thanks & Rgds
Heng03
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question