简体   繁体   中英

How can I convert a DateTime to a string with fractional seconds that is localized?

I have a DateTime object and I want to output the hour, minute, second, and fractional second as a string that is localized for the current culture.

There are two issues with this.

First issue is there is no standard DateTime format that will show the fractional seconds. I essentially want to know how to get the long time DateTime format but with fractional seconds.

I could of course get DateTimeFormatInfo.LongTimePattern and append ".fff" to it and pass it to the DateTime.ToString() , but some of the culture specific formats, US specifically, end with AM/PM. So it isn't that simple.

The second issue is that DateTime.ToString() does not appear to localize the number decimal separator. If I decided to just force each culture to use a hard coded custom time format it still will not create a localized string as the number decimal separator will not be culture specific.

To complicate matters further, some cultures have date time formats that use periods as part of their formatting. This makes it difficult to put a placeholder, for example the period and replace it with a culture specific decimal separator.

For now I have resorted to this workaround:

string format = string.Format("HH:mm:ss{0}fff",
    CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
string time = DateTime.Now.ToString(format);

Which I think should work for every culture that doesn't have the same decimal separator as the time separator, but that is an assumption.

Of Note: While it would be nice to have a solution to both issues, for my specific application I am more interested in localizing a custom date time format with fractional seconds than using a standard date time format.

First issue is there is no standard DateTime format that will show the fractional seconds. I essentially want to know how to get the long time DateTime format but with fractional seconds.

You might consider taking the long format and just replacing ":ss" with ":ss.fff", possibly using the culture-specific decimal separator:

string longPattern = culture.DateTimeFormat.LongTimePattern;
if (!longPattern.Contains(":ss"))
{
    // ???? Throw an exception? Test this with all system cultures, but be aware
    // that you might see custom ones...
}
// Only do it if the long pattern doesn't already contain .fff... although if
// it does, you might want to replace it using the culture's decimal separator...
if (!longPattern.Contains(".fff"))
{
    longPattern = longPattern.Replace(":ss", 
        ":ss" + culture.NumberFormat.DecimalSeparator + "fff");
}
string timeText = time.ToString(longPattern, culture);

To complicate matters further, some cultures have date time formats that use periods as part of their formatting. This makes it difficult to put a placeholder, for example the period and replace it with a culture specific decimal separator. Other than that though, it should end up putting the value in the right place. Do you definitely always want three digits though, even if the value is an exact second (or half a second, etc)? Use .FFF if not.

I suspect that's why the culture-specific decimal separator isn't used. I've found it odd myself - indeed, I've made Noda Time behave the same way, even though I'm not convinced it's right .

Ultimately, a lot of issues like this are fundamentally problematic: if a culture has no fixed way of representing "a time with fractional seconds" and you've got to represent fractional seconds, then the best you're going to be able to do is a bit of a kludge.

I think this will give you something close to what you require

var now = DateTime.Now;
var seconds = now.TimeOfDay.TotalSeconds % 60;
var timeText = string.Format(CultureInfo.GetCultureInfo("fr-FR"), 
                             "{0:d} {0:hh:mm:}{1:00.000}", now, seconds);

or just use

var timeText = string.Format(CultureInfo.CurrentCulture, 
                             "{0:d} {0:hh:mm:}{1:00.000}", now, seconds);

For example

  • fr-FR: 20/03/2012 10:20:10,088
  • en-GB: 20/03/2012 10:21:08.724
  • en-US: 3/20/2012 10:21:24.470

Generally I change the current culture and I do the operation I need.

System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
try
{
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    string timestr = DateTime.Now.ToString();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = before;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM