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
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
.
i am using vb.net in web applications
i put a page code that may be helping you dealing with dates
ASPX
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
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
精彩评论