1

how to store string value in date variable in vb.net

i am using the following code

dim dtBL as Date
txtBLDate.text="23/11/2010"
dtBL = Format(CDate(txtBLDate.Text), "MM/dd/yyyy")

but i am getting the error which says that 'Conversion from string "23/11/2010" to type 'Date' is not valid.' please advice on this

3 Answers 3

3

Two rules of thumb I give everyone with VB.Net:

  • Turn Option Strict On
  • Abandon the Microsoft.VisualBasic-Namespace ASAP

To answer your question, your date is in the format dd/MM/yyyy and not MM/dd/yyyy.

Sign up to request clarification or add additional context in comments.

2 Comments

Why abandon the Microsoft.VisualBasic namespace? It is part of VB and won't go away. Definitely abandon the Microsoft.VisualBasic.Compatibility namespaces, however.
@Chris Dunaway: Because importing Microsoft.VisualBasic will also import the 'legacy' VB6-Functions which will show up in AutoCompletion. Importing and especially using Microsoft.VisualBasic will turn VB.NET into an ugly hybrid of Object-Oriented and Functional language features and defeats IMO the whole purpose of VB.NET.
1

i am using vb.net in web applications

i put a page code that may be helping you dealing with dates


ASPX


alt text


code behind


Partial Class DateFormatConversions Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    lblDate.Text = Today.ToString("M/d/yyyy")
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    lblDate.Text = Today.ToString("MM/dd/yyyy")
End Sub 
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
    lblDate.Text = Today.ToString("d/M/yyyy")
End Sub

Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
    lblDate.Text = Today.ToString("dd/MM/yyyy")
End Sub

Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
    Dim DTFI As New System.Globalization.DateTimeFormatInfo
    DTFI.ShortDatePattern = DropDownList1.SelectedValue

    Dim addedDate As DateTime
    addedDate = DateTime.Parse(TextBox1.Text, DTFI)

    lblDateOutput.Text = addedDate.ToLongDateString
End Sub End Class

4 Comments

Can you please fix the wording formatting of your post, it's hardly readable. Thanks.
i am also adding a function that i use in date conversion, i put it as another answer, check it if you can get some ideas from it. sorry again for "jammed" posts
thanks mokokamello for your comment i have solved the issue by using the code below Dim BLMonth, BLDay, BLYear As String Dim dtBL As Date BLDay = strBLDate.Substring(0, InStr(strBLDate, "/") - 1) BLMonth = strBLDate.Substring(InStr(strBLDate, "/"), 3) BLYear = strBLDate.Substring(InStrRev(strBLDate, "/")) dtBL = CDate(BLMonth & "/" & BLDay & "/" & BLYear & "")
press space bar 4 times will tell the editor that code is coming. for example: spaceX4 then type "dim i as integer"
0
Imports Microsoft.VisualBasic  Imports System.Globalization  Public Class DatumKonvert1 Public Shared Function DK1(ByVal myDMstring As String) As Date 
Dim source As String = myDMstring 
Dim d As DateTime = DateTime.ParseExact(source, "d'/'M'/'yyyy", CultureInfo.InvariantCulture) 
Dim resultMydate As String = d.ToString("M'/'d'/'yyyy") 
Dim mdx = DateTime.ParseExact(resultMydate, "M'/'d'/'yyyy", CultureInfo.InvariantCulture) 
Return mdx     End Function End Class 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.