Question

Does anyone know of a way to declare a date constant that is compatible with international dates?

I've tried:

' not international compatible
public const ADate as Date = #12/31/04#

' breaking change if you have an optional parameter that defaults to this value
' because it isnt constant.
public shared readonly ADate As New Date(12, 31, 04)
Was it helpful?

Solution

If you look at the IL generated by the statement

public const ADate as Date = #12/31/04#

You'll see this:

.field public static initonly valuetype [mscorlib]System.DateTime ADate
.custom instance void [mscorlib]System.Runtime.CompilerServices.DateTimeConstantAttribute::.ctor(int64) = ( 01 00 00 C0 2F CE E2 BC C6 08 00 00 )

Notice that the DateTimeConstantAttribute is being initialized with a constructor that takes an int64 tick count. Since this tick count is being determined at complile time, it seems unlikely that any localization is coming into play when this value is initialized at runtime. My guess is that the error is with some other date handling in your code, not the const initialization.

OTHER TIPS

According to the Microsoft documentation,

"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."

Are you saying that this is not correct and the parsing is affected by the current locale?

Edit: Did you try with a 4-digit year?

Once you have data into Date objects in VB, you don't have to worry about globalization until you compare something to it or try to export it.

This is fine:

Dim FirstDate as Date = Date.UtcNow() 'or this: = NewDate (2008,09,10)'
Dim SecondDate as Date

SecondDate = FirstDate.AddDays(1)

This pulls in the globalization rules and prints in the current thread's culture format:

HeaderLabel.Text = SecondDate.ToString()

This is bad:

Dim BadDate as Date = CDate("2/20/2000")

Actually--even that is OK if you force CDate in that case to use the right culture (InvariantCulture):

Dim OkButBadPracticeDate as Date = CDate("2/20/2000", CultureInfo.InvariantCulture)

If you want to force everything to a particular culture, you need to set the executing thread culture and UI culture to the desired culture (en-US, invariant, etc.).

Make sure you aren't doing any work with dates as strings--make sure they are actual Date objects!

Ok right, I understand more where you are coming from..

How about:

  • Create a static method that returns the date constant. This overcomes the international issue since it is returned as the specific DateTime value.
  • Now I remember optional params from my VB6 days, but can you not just overload the method? If you are using the overloaded method without the date, just pull it from the static?

EDIT: If you are unsure what I mean and would like a code sample, just comment this post and I will chuck one on.

OK, I am unsure what you are trying to do here:

  • The code you are posting is NOT .NET, are you trying to port?
  • DateTime's cannot be declared as constants.
  • DateTime's are a data type, so once init'ed, the format that they were init'ed from is irrelevant.
  • If you need a constant value, then just create a method to always return the same DateTime.

For example:

public static DateTime SadDayForAll()
{
    return new DateTime(2001, 09, 11);
}

Update

Where the hell are you getting all that from?!

  • There are differences between C# and VB.NET, and this highlights one of them.
  • Date is not a .NET data type - DateTime is.
  • It looks like you can create DateTime constants in VB.NET but there are limitations
  • The method was there to try and help you, since you cannot create a const from a variable (i.e. optional param). That doesn't even make sense.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top