Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display date string with only day and month based on culture?

I need to display some dates on windows phone with day and month only.

For example, January 1st, for American users I want the text to be '1/31' and for UK users I want it to be '31/1'.

What's the easiest way to achieve something like this?

UPDATE: My app will be available in a lot of countries. Do I have to specify each culture to get what I want?

For example,

de-DE Culture                         01.10
en-US Culture                         10/1
es-ES Culture                         01/10
fr-FR Culture                         01/10

The left column contains some countries my app will support, the right column is how I want my date text to be.

Is there any generic way I can achieve this?

like image 689
Jessica Avatar asked Nov 23 '25 10:11

Jessica


1 Answers

// 13 August
string americanDate = theDate.ToString("m", new CultureInfo("en-us"));

// August 13
string ukDate = theDate.ToString("m", new CultureInfo("en-gb"));

For full list of available formats, see MSDN.

EDIT:

In your app, don't manually specify a CultureInfo instance: use the default culture for DateTime.ToString, which is the culture of the executing thread.

So your code becomes:

string cultureSpecificDate = theDate.ToString("m");

where "m" is a date format you choose in the list of available formats. The one you want doesn't appear to be a standard one provided by the DateTimeFormatInfo class. Couldn't you choose a supported pattern in the list below?

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

  d     1/3/2002
        M/d/yyyy (ShortDatePattern)

  D     Thursday, January 03, 2002
        dddd, MMMM dd, yyyy (LongDatePattern)

  f     Thursday, January 03, 2002 12:00 AM

  F     Thursday, January 03, 2002 12:00:00 AM
        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

  g     1/3/2002 12:00 AM

  G     1/3/2002 12:00:00 AM

  m     January 03
        MMMM dd (MonthDayPattern)

  M     January 03
        MMMM dd (MonthDayPattern)

  o     2002-01-03T00:00:00.0000000

  r     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

  t     12:00 AM
        h:mm tt (ShortTimePattern)

  T     12:00:00 AM
        h:mm:ss tt (LongTimePattern)

  u     2002-01-03 00:00:00Z
        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

  U     Thursday, January 03, 2002 8:00:00 AM

  y     January, 2002
        MMMM, yyyy (YearMonthPattern)

  Y     January, 2002
        MMMM, yyyy (YearMonthPattern)

*/
like image 126
ken2k Avatar answered Nov 25 '25 23:11

ken2k