tags:

views:

310

answers:

3

I'm working in a web application using VB.NET. There is also VisualBasic code mixed in it, in particular the Date variable and the Month function of VB.

The problem is this part:

Month("10/01/2008")

On the servers, I get 10 (October) as the month (which is supposed to be correct). On my machine, I get 1 (January) (which is supposed to be wrong).

Two of my colleagues (on their own machines) get different answers, one got 1, the other got 10.

The question is, why is this so?

On my end, I can solve the problem by using .NET's DateTime's Parse (or ParseExact) function to force everything to be "dd/MM/yyyy" format. This works. I'm just wondering why there's an inconsistency.

Extra info: I know the parameter for Month function is supposed to be a Date variable. The code used a string as parameter, and Option Strict was off, and the developers mainly let VB do its own conversion thing. (Legacy code maintenance has a lot of inertia...)

If it helps, the version of Microsoft.VisualBasic.dll on the servers is 7.10.6310.4 (under the Framework folder v1.1.4322). The version on mine (and my 2 colleagues') machine is 7.10.6001.4.

Edit: Regional settings for all machines already set to dd/MM/yyyy format (short date format).

+5  A: 

This normally has to do with the regional settings, and more specifically the date/time formats. If you set these formats so that they are all the same on the machines you're testing on, the results should be consistent.

Your idea of using ParseExact is definitely the better solution to go with, IMHO.

Darksider
A: 

The regional settings for all machines (servers and developers) are already set to dd/MM/yyyy. I understand VB takes its default from the short date format. This is why it's perplexing...

Vincent Tan
+2  A: 

This is because the runtime has to convert your given value "10/01/2008" which is indeed a string implicitly to the DateTime datatype.

When converting strings to dates and the other way round, the string format depends on the locale settings of windows.

See this link on msdn.

In this article a way to specify a date literal which is independent of your locale settings:

Just enclose the date with the sign # and specify it in the form mm/dd/yyyy:

So the code

Month(#10/01/2008#)

should give you the answer 10 on any machine.

Ther a two more worarounds given in that msdn article:

1. Use the Format Function with predifned Date/Time Format

To convert a Date literal to the format of your locale, or to a custom format, supply the literal to the Format Function, specifying either Predefined Date/Time Formats (Format Function) or User-Defined Date/Time Formats (Format Function). The following example demonstrates this.

MsgBox("The formatted date is " & Format(#5/31/1993#, "dddd, d MMM yyyy"))

2. Use the DateTime-Class Constructor to construt the right DateTime value

Alternatively, you can use one of the overloaded constructors of the DateTime structure to assemble a date and time value. The following example creates a value to represent May 31, 1993 at 12:14 in the afternoon.

Dim dateInMay As New System.DateTime(1993, 5, 31, 12, 14, 0)

Jan
Thanks for your info. The thing is, I can't use the literal format because the date string is taken from Request.QueryString, and used like soDim s As String = Request.QueryString("currdate")Dim iMonth As Int32 = Month(s)
Vincent Tan
I would parse the request-string and use Method 2. (DateTime Constructor).
Jan