Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .NET, how do I get the order of YYYY, MM and DD for a given culture

Designing a culture independent birthdate input consisting of three select: year, month and day.

In .NET, how do I get the correct display order of the three for a given culture, ie.:

┌─────────────┬─┐  ┌──────────────┬─┐  ┌────────────┬─┐
│ Select year │v│  │ Select month │v│  │ Select day │v│  
└─────────────┴─┘  └──────────────┴─┘  └────────────┴─┘

┌─────────────┬─┐  ┌──────────────┬─┐  ┌────────────┬─┐
│ Select day  │v│  │ Select month │v│  │ Select year│v│  
└─────────────┴─┘  └──────────────┴─┘  └────────────┴─┘

or

┌─────────────┬─┐  ┌──────────────┬─┐  ┌────────────┬─┐
│ Select month│v│  │ Select day   │v│  │ Select year│v│  
└─────────────┴─┘  └──────────────┴─┘  └────────────┴─┘

* UPDATE * Thanks to all for your answers, it made me come up with this little function:

  Public Function GetDateElementAtPos(ByVal pos As Integer) As String
    Return Mid(Me.myCultureInfo.DateTimeFormat.ShortDatePattern.Split(Me.myCultureInfo.DateTimeFormat.DateSeparator)(pos), 1, 1).ToLower
  End Function

Where myCultureInfo represents a initialized Globalization.CultureInfo. I use it like this:

Dim s as new stringbuilder
For i As Integer = 0 To 2
  Select Case GetDateElementAtPos(i)
    Case "y"
      s.append(**year select html goes here**)
    Case "m"
      s.append(**month select html goes here**)
    Case "d"
      s.append(**day select html goes here**)
  End Select
Next 
like image 852
Muleskinner Avatar asked Oct 11 '11 12:10

Muleskinner


1 Answers

Here you go:

CultureInfo c = ....;
var dtf = c.DateTimeFormat;
var fs = dtf.ShortDatePattern;
like image 173
leppie Avatar answered Sep 25 '22 20:09

leppie