Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CDate conversion error

Posted on 2003-12-06
12
Medium Priority
?
1,569 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
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 
LVL 28

Assisted Solution

by:mmarinov
mmarinov earned 75 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 75 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 75 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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)…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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