views:

27

answers:

2

Does anybody know how to display the date of a datetime based on a CurrentCulture but the Time follow this pattern "HH:mm:ss.fff" ?

I've tried to use:

DateTime NewDate = DateTime.Now; NewDate.ToString(CultureInfo.CurrentCulture);
It results: 4-6-2010 14:49:41.
Expected: 4-6-2010 14.49.41,495.

For the Time, I also need to show the miliseconds but the Date format must be still based on the culture.

+1  A: 
  NewDate.Date.ToString(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern) + 
     NewDate.TimeOfDay.ToString("HH:mm:ss.fff");

From MSDN:

This method uses formatting information derived from the current culture. In particular, it combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo object returned by the Thread.CurrentThread.CurrentCulture.DateTimeFormat property

David Neale
A: 
string s = String.Format("{0} {1}", NewDate.ToString("d"), NewDate.ToString("HH:mm:ss.fff"));

If do not wish to use the current culture to format your datestring you can create an instance of CultureInfo and pass it to your ToString method

CultureInfo ci = new CultureInfo("en-US");
string s = String.Format("{0} {1}", NewDate.ToString("d", ci ), NewDate.ToString("HH:mm:ss.fff"));
Morten Anderson
That's it!! Thank you very much Morten.just need to add the miliseconds:string s = NewDate.ToString("d") + " " + NewDate.ToString("HH:mm:ss.fff");
Albertus
great - I have added milliseconds now :-)
Morten Anderson