DateTime.ToString(“MM/dd/yyyy HH:mm:ss.fff") 导致类似“09/14/2013 07.20.31.371"的结果 [英] DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff") resulted in something like "09/14/2013 07.20.31.371"

查看:18
本文介绍了DateTime.ToString(“MM/dd/yyyy HH:mm:ss.fff") 导致类似“09/14/2013 07.20.31.371"的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WP8 应用程序,它会将当前时间发送到网络服务.

I have a WP8 app, which will send the current time to a web service.

我通过调用

DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff")

对于大多数用户来说,它工作得很好,并为我提供了正确的字符串,例如 "09/10/2013 04:04:31.415".但是对于某些用户,结果字符串类似于 "09/14/2013 07.20.31.371",这会导致我的网络服务出现问题.

For most users it works great and gives me the correct string like "09/10/2013 04:04:31.415". But for some user the resulted string is something like "09/14/2013 07.20.31.371", which causes problem in my web service.

是不是因为某些文化格式问题?如何确保结果字符串由冒号而不是点分隔?

Is it because some culture format issue? How can I make sure the result string is delimited by colon instead of dot?

推荐答案

是不是因为文化格式问题?

Is it because some culture format issue?

是的.您的用户必须处于时间分隔符为点的文化中.两个:"和/";在自定义日期和时间格式中以文化敏感的方式解释.

Yes. Your user must be in a culture where the time separator is a dot. Both ":" and "/" are interpreted in a culture-sensitive way in custom date and time formats.

如何确保结果字符串由冒号而不是点分隔?

How can I make sure the result string is delimited by colon instead of dot?

我建议指定CultureInfo.InvariantCulture:

string text = dateTime.ToString("MM/dd/yyyy HH:mm:ss.fff",
                                CultureInfo.InvariantCulture);

或者,您可以只引用时间和日期分隔符:

Alternatively, you could just quote the time and date separators:

string text = dateTime.ToString("MM'/'dd'/'yyyy HH':'mm':'ss.fff");

...但这会给你带来有趣的"如果您让用户在默认日历系统不是公历的文化中运行,您可能不会期望结果.以如下代码为例:

... but that will give you "interesting" results that you probably don't expect if you get users running in a culture where the default calendar system isn't the Gregorian calendar. For example, take the following code:

using System;
using System.Globalization;
using System.Threading;

class Test
{
    static void Main()        
    {
        DateTime now = DateTime.Now;
        CultureInfo culture = new CultureInfo("ar-SA"); // Saudi Arabia
        Thread.CurrentThread.CurrentCulture = culture;
        Console.WriteLine(now.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
    }
} 

产生以下输出(2013 年 9 月 18 日):

That produces output (on September 18th 2013) of:

11/12/1434 15:04:31.750

我猜您的网络服务会对此感到惊讶!

My guess is that your web service would be surprised by that!

我实际上建议不仅使用不变区域性,而且更改为 ISO-8601 日期格式:

I'd actually suggest not only using the invariant culture, but also changing to an ISO-8601 date format:

string text = dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture);

这是一种更为全球接受的格式 - 它也是可排序的,并且使月和日的顺序显而易见.(而 06/07/2013 可以解释为 6 月 7 日或 7 月 6 日,具体取决于读者的文化.)

This is a more globally-accepted format - it's also sortable, and makes the month and day order obvious. (Whereas 06/07/2013 could be interpreted as June 7th or July 6th depending on the reader's culture.)

这篇关于DateTime.ToString(“MM/dd/yyyy HH:mm:ss.fff") 导致类似“09/14/2013 07.20.31.371"的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆