.NET Date Const (with globalization)

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

I 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)
+3
source share
5 answers

If you look at the IL generated by the statement

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

You will see the following:

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

Note that DateTimeConstantAttribute is initialized by a constructor that accepts int64 samples. Since this number of samples is determined at run time, it seems unlikely that any localization comes into play when this value is initialized at run time. I suppose the error is due to some other date processing in the code, not const initialization.

+6
source

Microsoft,

" (# #). M/d/yyyy, # 5/31/1993 #. ."

, , ?

: 4- ?

+4

Date VB, , .

:

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

SecondDate = FirstDate.AddDays(1)

:

HeaderLabel.Text = SecondDate.ToString()

:

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

- , CDate (InvariantCulture):

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

, (en-US, ..).

, - , Date!

+1

, , :

  • , , . NET, ?
  • DateTime .
  • DateTime - , init'ed , , .
  • , , DateTime.

:

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

Update

, , ?!

  • # VB.NET, .
  • .NET - DateTime.
  • , DateTime VB.NET, .
  • , ( ). .
0

, , .

:

  • , . , DateTime.
  • VB6, ? , ?

: , , , .

0

Source: https://habr.com/ru/post/1696820/


All Articles