How to convert string to date ..?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 73857 Views - Last Post: 16 May 2010 - 06:29 AM Rate Topic: -----

#1 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

How to convert string to date ..?

Posted 13 May 2010 - 04:33 PM

Hi...
I write the program first the program required from user to enter the date of birth in textbox
then click the button
the button is add 21 or any number to the birth date ..

when i write this program some of errore is Appearance !!
the erroe is :

sub add()
dim number as date
number=Cdate(txtbox1.text) ـــ> Here error is(Conversion from string "5/13/2010" to type 'Date' is not valid.)
number=number+21 ـــــ> and here is error (operator'+'is not defined for types 'date' and 'integer' )
txtbox2.text=number

end sub

Please Helpe me .. :)

I Use vb.net (express)

This post has been edited by fatma_ma: 13 May 2010 - 04:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to convert string to date ..?

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to convert string to date ..?

Posted 13 May 2010 - 04:42 PM

Not sure why you're getting an error with CDate with that string but to add (i assume 21 days) you would use
number = number.adddays(21)


You might try textbox1.text.trim on the first issue to ensure that you don't have any leading or trailing spaces that might be throwing off the conversion.

This post has been edited by CharlieMay: 13 May 2010 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 PDUNZ   User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 376
  • Joined: 17-July 09

Re: How to convert string to date ..?

Posted 13 May 2010 - 05:02 PM

To check if the date in the textbox is in a valid date format, you could use something like this (where "EnteredDate" is the variable where the checked date is stored for use).

Dim EnteredDate as Date = Date.ParseExact(TextBox1.Text.ToString(), "dd/mm/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)

Was This Post Helpful? 0
  • +
  • -

#4 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 13 May 2010 - 05:11 PM

Hi ...

Thank you very much charliemay
I Try this method then the run is ok.. :^:

:) :)

Thank you very very much
I wish you success
......

This post has been edited by fatma_ma: 13 May 2010 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 13 May 2010 - 05:17 PM

thnk you PDUNZ ..
:)
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to convert string to date ..?

Posted 13 May 2010 - 06:23 PM

View Postfatma_ma, on 13 May 2010 - 06:11 PM, said:

Thank you very very much
I wish you success

Your wish for my success was granted the moment my reply solved your problem ;)
Glad I could help.
Was This Post Helpful? 0
  • +
  • -

#7 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 13 May 2010 - 07:11 PM

Hi ... charliemay
I Try to write the program
this program is save some information in database ..some of this information is date .
the type of field of date in database is (date/time)

before i store the date in database i add to it 21

then i need to compare the date if the now() is greater than Retrieved date from database or no and desplay the result .
but when i write it in vb.net the erroe is Appearance..!!
beacause the vb.net use the arabic date
for example(30/5/1431)
but the database (Access) use the english date for example(14/5/2010)
when i enter the date in textbox1.text and then save it in database, the date is stored as english

my code is :

1-:i enter the date in textbox1.text (as englisg or arabic is normal beacause it as string here)
2-:i add to it 21 (i use the method that give me before one hour ago)
3-:i save the result from step 2 in my database
4-:i neen to Retrieval the date from database and compare it

dim number as date
number = Rederee.GetDateTime(12)ــــ> the error is apeare here
if now() >= number then ــــ> and here
msgbox("hello")
end if

please help me how to convert the date to english date or arabic date before and after add in database
:helpsmilie:

..............................

This post has been edited by fatma_ma: 13 May 2010 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to convert string to date ..?

Posted 13 May 2010 - 07:20 PM

List the SQL statement that you're using the pull the date from the database. One thing to keep in mind is that in Access dates need to be enclosed in # instead of ' so to select a date from the database you would use a similar sql statement as follows:

"SELECT * From MyTable WHERE fldDate = #05/13/2010#"

If you list the code you're using, myself or someone should be able to help.
Was This Post Helpful? 0
  • +
  • -

#9 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 13 May 2010 - 07:34 PM

No i use another syntax
i use this code to save the date in database:
"insert into table (my_date)values ('" & textbox.text & "')"

when i Retrieval the date from database i use the another key:
"select * from table where my_id=" & myID

This post has been edited by fatma_ma: 13 May 2010 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to convert string to date ..?

Posted 14 May 2010 - 04:33 AM

That's fine, but the same applies to the insert statement.
INSERT INTO Table(my_date) VALUES (#" & textbox.text & "#)"

You really should use parameters to insert values into the database it's a lot safer.
Was This Post Helpful? 0
  • +
  • -

#11 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 14 May 2010 - 04:49 PM

Hi .... charliemay

I Will Try It Now :)
Was This Post Helpful? 0
  • +
  • -

#12 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 14 May 2010 - 05:27 PM

Thank you very very much charliemay

i try it , the run is success
iam very happy beacause my program run is success
thank you for help me :flowers:
Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to convert string to date ..?

Posted 14 May 2010 - 06:21 PM

No Problem, glad to be able to help :)
Was This Post Helpful? 0
  • +
  • -

#14 fatma_ma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-May 10

Re: How to convert string to date ..?

Posted 15 May 2010 - 12:14 PM

Hello .. charliemay
my vb.net (express) is Receives the date as (dd/mm/yyyy)
but my database (microsoft office Access) Deal with date as (mm/dd/yyyy)

How to convert my vb.net from (dd/mm/yyyy) to (mm/dd/yyyy) ??
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to convert string to date ..?

Posted 15 May 2010 - 06:28 PM

In the database, you can set the format of the date in Access. If you right click on the table and select design view you should see the fields in your table and their types. Click on the date field and in the lower pane, you should see format, change it to dd/mm/yyyy.

This post has been edited by CharlieMay: 15 May 2010 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2