• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

datetime

How can I convert string to datetime? If string is blank, I will to leave it at "". I want to insert datetime to sql as blank.

from 2010:08:11 20:05:18"
to   2010-08-11 20:05:18.000

Conversion from string "2010:08:11 20:05:18" to type 'Date' is not valid.
2010-08-11 20:05:18.000
0
VBdotnet2005
Asked:
VBdotnet2005
1 Solution
 
CodeCruiserCommented:
Sql would not accept blank in date column. You would need to insert dbnull instead.

You can try

If String.IsNullOrEmpty(mydate) Then
   'insert DBNull.Value
Else
   DateTime.Parse(mydate)
End If
0
 
VBdotnet2005Author Commented:
can I do it like this?

mydatestring = DateTime.ParseExact(mydatestring, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture)
0
 
nishant joshiTechnology Development ConsultantCommented:
codecruiser is correct might some change required...

If String.IsNullOrEmpty(mydate) Then
   'insert DBNull.Value
Else
   DateTime.Parse(mydate).getDate()
End If


and of this will not work then try with substring and extract date from it.;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CodeCruiserCommented:
Yes you can. but store it in a date not string.
0
 
CodeCruiserCommented:
@nishantcomp2512

Does DataTime have a method called GetDate()?
0
 
Jacques Bourgeois (James Burger)Commented:
A date cannot have : as a date separator. This is why your conversion does not work. You need to replace it by the date separator on the system that is used to make the conversion. In a windows application, that would be the following:
Dim theString As String = "2010:08:11 20:05:18"
Dim dateSeparator As String = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.DateSeparator
Dim theDate As Date

theString = theString.Substring(0, 4) & dateSeparator & theString.Substring(5, 2) & dateSeparator & theString.Substring(8, 11)
theDate = CDate(theString)

Open in new window


The Date can never be "" however. A date need to be a Date. "" is not a valid value. You could work like this:
If theString.length=0 then
    theDate = New Date 'Reinitialize the date to its default value if the string is empty
else
    theString = theString.Substring(0, 4) & dateSeparator & theString.Substring(5, 2) & dateSeparator & theString.Substring(8, 11)
    theDate = CDate(theString)[/
End If

Open in new window

And when you want to display it as a String:
If theDate = New Date Then
    MessageBox.Show("")
Else
    MessageBox.Show(theDate.ToString)
End If

Open in new window

0
 
nishant joshiTechnology Development ConsultantCommented:
sorry use DateTime.date to get date no methods like getDate()..:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now