Solved

CDate conversion error

Posted on 2003-12-06
12
1,557 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
Comment Utility
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
Comment Utility
Hi mmarinov

Your suggestion have syntax error.


Thanks & Rgds
Heng03
0
 

Author Comment

by:heng03
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

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

Author Comment

by:heng03
Comment Utility
Hi
0
 
LVL 1

Expert Comment

by:MarcGraff
Comment Utility
Sweet! Thanks!

   - Marc
0
 
LVL 27

Expert Comment

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

Dabas
0
 

Author Comment

by:heng03
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

17 Experts available now in Live!

Get 1:1 Help Now