Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 7th, 2009, 01:14 PM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Unhappy Problem with date type

Hi All,

I want convert the string type to date type but i am getting error.

Dim sDate as String = "12/15/2008"

Dim dAttrDate AsDate = "#" & sDate & "#"

the below error :

System.InvalidCastException: Conversion from string "#12/15/2008#" to type 'Date' is not valid.
at Microsoft.VisualBasic.CompilerServices.Conversions .ToDate(String Value)

I am doing this because i want to fomat the date in below format after converting to date Type:

Dim dDate As New java.util.Date(CInt(Format(dAttrDate, "yyyy")) - 1900, _
CInt(Format(dAttrDate, "MM")) - 1, _
CInt(Format(dAttrDate, "dd")), _
CInt(Format(dAttrDate, "hh")), _
CInt(Format(dAttrDate, "mm")), _
CInt(Format(dAttrDate, "ss")))

How to avoid the above error.

Thanks
Nelly
 
Old December 7th, 2009, 01:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

This is very confusing..

You are converting something to a Java date????????!?!??!!?!????

Are you using asp.net????

why don't create a datetime object with the date you need?? (use it as a parameter in the constructor)...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old December 7th, 2009, 04:05 PM
Registered User
 
Join Date: Oct 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Just use Ctype function

nelly78
I would just use the CType(datetimeString,Date) function
Code:
Dim sDate as String = "12/15/2008" 

Dim dAttrDate As Date = CType(sDate, Date)
If you want to avoid having errors on run time depending on non-convertibility of sDate into a date, you may prefer to do encapsulate this in a Try ... Catch ... End Try routine like this


Code:
Dim sDate as String = "12/15/2008" 

Try
    Dim dAttrDate As Date = CType(sDate, Date)
Catch

End Try
 
Old December 9th, 2009, 10:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

With that in mind, the OP can just use Cdate...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Type checking NEO1976 XSLT 5 February 1st, 2007 10:37 AM
date type queries mohiddin52 Access 5 July 29th, 2005 03:32 PM
date type queries mohiddin52 Access 1 January 11th, 2005 01:41 PM
Getting Problem in DATE/TIME Data type in MS Acces selam70 Classic ASP Databases 2 September 4th, 2004 02:31 PM
date type queries mohiddin52 Access 1 March 19th, 2004 03:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.