Solved

CDate conversion error

Posted on 2003-12-06
12
1,558 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
  • 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now