w0051977
w0051977

Reputation: 15817

CDATE behaviour not consistent with documentation (for me)

Please take a look at the following webpage: http://msdn.microsoft.com/en-us/library/3eaydw6e.aspx:

" You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#. This requirement is independent of your locale and your computer's date and time format settings. "

Now see the code below:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim str As String = "1-2-1999"
        Dim dte As Date = CDate(str)
        MsgBox(dte.Month)
    End Sub

Questions:

1) The messagebox prints: 2. I would expect it to print 1 based on the following quote: "You must specify the date value in the format M/d/yyyy".
2) The string literal is: 1-2-1999 and not #1-2-1999#, yet the quote says: "You must enclose a Date literal within number signs (# #)"

Upvotes: 0

Views: 37

Answers (1)

From MSDN: You must enclose a Date literal within number signs (# #). You must specify the date value in the format M/d/yyyy, for example #5/31/1993#. That isnt what you did. You created a string, then converted it to a date. To create a Date variable using a literal:

Dim dte As Date = #1/2/1999#
Console.WriteLine(dte.Month)      ' prints 1  

Upvotes: 2

Related Questions