Solved

Force the date format to always be en-US

Posted on 2007-11-24
3
251 Views
Last Modified: 2013-12-16
Hi,

I need to force the date format to always display it  in en-US format... Since I'm not using en-US as default on my machine, I'm getting the date displayed in another format.

What I exactly need is:

1. Displaying dateTime.ToShortDateString() in en-US format.
2. Displaying dateTime.ToShortTimeString() in fr-FR format.

Below is the code I'm using. How can I modify it?

Thanks a lot.
// This is an example of a UNIX timestamp for the date/time 11-04-2005 09:25.
double timestamp = 1113211532;
 
// First make a System.DateTime equivalent to the UNIX Epoch.
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
 
// Add the number of seconds in UNIX timestamp to be converted.
dateTime = dateTime.AddSeconds(timestamp);
 
// The dateTime now contains the right date/time so to format the string,
// use the standard formatting methods of the DateTime object.
string printDate = dateTime.ToShortDateString() +" "+ dateTime.ToShortTimeString();
 
// Print the date and time
System.Console.WriteLine(printDate);

Open in new window

0
Comment
Question by:EEssam
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20343867
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20343897
use:
string printDate = dateTime.ToString("F", new CultureInfo("en-US"));

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20343942
ToShortDateString uses the "d" format string and ToShortTimeString uses the "t" format string, so you want to do like this:

CultureInfo us = new CultureInfo("en-US");
CultureInfo fr = new CultureInfo("fr-FR");
string printDate = dateTime.ToString("d", us) +" "+ dateTime.ToString("t", fr);
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question