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"

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

问题描述

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

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" ,这会导致我的Web服务出现问题.

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

我的猜测是您的Web服务会为此感到惊讶!

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);

这是一种全球通用的格式-它也可以排序,并使月份和日期顺序显而易见.(鉴于读者的文化,2013年6月7日可以解释为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天全站免登陆