-2

this code return to me 'M/d/yyyy' but I need 'mm/dd/yyyy' how do this?

((CultureInfo)Session["culture"]).DateTimeFormat.ShortDatePattern)
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
Mediator
  • 13,836
  • 33
  • 104
  • 177
  • Not sure to understand: you want to get the format from the culture, and you expect a fixed, hard-coded format to be returned? – ken2k Dec 21 '12 at 16:49
  • If you want it to return the specific format you need to set up it first. (But it gives me no sense). `(CultureInfo)Session["culture"]).DateTimeFormat.ShortDatePattern = "mm/dd/yyyy"` – mipe34 Dec 21 '12 at 16:51
  • No, I need format not Dec/21/2012 and I need 12/21/2012 – Mediator Dec 21 '12 at 16:51
  • @simplydenis - What is `Session["culture"]` and where does it get set? – Mike Christensen Dec 21 '12 at 16:59
  • You need to clarify a bit better. If you always want `"MM/dd/yyyy'`, then you can simply skip `CultureInfo` using a `ToString(string format)` overload. However if you need to some how customize a culture's format string without completely replacing it, you will need to provide more detail as to what that entails. – Guvante Dec 21 '12 at 20:57

3 Answers3

1

That dosn't make much since... The pattern you want is for a specific culture.. For instance:

DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US").DateTimeFormat.ShortDatePattern);

Will return mm/dd/yyyy but

DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat.ShortDatePattern);

will return dd/mm/yyyy.

From the code you added, it's not very clear - it seems you're getting a specific culture but then want to override the ShortDatePattern...

If this is true, why use the Culture received in the session in the first place? Use whatever pattern you want without regarding the culture. e.g. DateTime.Now.ToString("MM/dd/yyyy");

(Anyways, it IS possible to override, as ShortDatePattern can be set, just like @mipe34 showed in the comments)

Blachshma
  • 16,379
  • 4
  • 51
  • 66
0

You can override how dates are displayed by specifying the template you want in the ToString method:

DateTime.Now.ToString("MM/dd/yyyy")) //Will display "12/21/2012"

You appear to be using a CultureInfo object, which contains default formats (and other cultural information such as currency) and is provided by the operating system.

Calling DateTime.Now.ToShortDateString() by itself will use the format specified by ShortDatePattern in the current thread's culture, which can be set with something like:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Mike Christensen
  • 77,193
  • 46
  • 189
  • 300
0

You mean you want to change the property? This is simple:

((CultureInfo)Session["culture"]).DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";

However, if the CultureInfo happens to be read-only, it will throw run-time. In that case maybe you can assign a new CultureInfo object? If you can, do it like this:

// get reference
var ci = (CultureInfo)Session["culture"];
// clone and reassign to get rid of read-only:
ci = (CultureInfo)(ci.Clone());
// change property:
ci.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
// make read-only again
ci = CultureInfo.ReadOnly(ci);
// assign back to your "source":
Session["culture"] = ci;

After that, when people call someDateTimeValue.ToShortDateString() on some new thread whose CurrentCulture is taken from Session["culture"], they will get your new format.

Note: "m" or "mm" is minutes, and "M" or "MM" is months. Also "/" is the DateSeparator of the culture (which might translate to a dash (-) or something else in some cultures).

Jeppe Stig Nielsen
  • 54,796
  • 9
  • 96
  • 154