Solved

CDate conversion error

Posted on 2003-12-06
12
1,566 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
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

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…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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