String.Format on Japanese Windows

S

schouwla

Hi,

I want to create dates in the following format: dd-MMM-yyyy e.g. 13-
Mar-2007

But are seeing some problems when running on a Japanese Windows XP.
Regional settings are set to JP.

Currently I am doing this
DateTime dat = DateTime.Now;
return String.Format("{0:dd}-{0:MMM}-{0:yyyy}", date);

US Windows returns 27-Feb-2007
Japanese Windows return 27-2-2007

So I tried to come up with a solution to that fixes but could only
find half of the solution.
CultureInfo ci = new CultureInfo("en-US");
datestring = dat.ToString("G", ci);
Console.WriteLine("en-US : " + datestring.Remove(datestring.IndexOf('
')));

This works on both versions but changed the date format to mm/dd/yyyy

When I do this:
DateTime dt = DateTime.Parse(datestring);
Console.WriteLine(dt.ToString("dd-MMM-yyyy"));
I am back to where I started!

Any help is much appeciated...

Regards
Lars Schouw
 
S

schouwla

Thort answer .. This does the stunt..
return date.ToString("dd-MMM-yyyy", new CultureInfo("en-US"));

Long answer:

CultureInfo ci = new CultureInfo("en-US");
DateTime dtn = DateTime.Now;
string datestring = dtn.ToString("d", ci);
Console.WriteLine("en-US : " + datestring);
DateTime dt = DateTime.Parse(datestring);
Console.WriteLine(dt.ToString("dd-MMM-yyyy"));
// Japanese Windows
// en-US : 2/27/2007
// 27-2-2007

// US Windows
// en-US : 2/27/2007
// 27-Feb-2007

Console.WriteLine(dtn.ToString("dd-MMM-yyyy", ci));
Console.WriteLine(dt.ToString("dd-MMM-yyyy"), ci);
// Japanese Windows
// 27-Feb-2007
// 27-2-2007

// US Windows
// 27-Feb-2007
// 27-Feb-2007
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top