达网络 - 则DateTime.ToString(QUOT; MM / DD / YYYY HH:MM:SS.FFF")导致类似" 2013年9月14日07.20.31.371" [英] .Net - DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff") resulted in something like "09/14/2013 07.20.31.371"

查看:375
本文介绍了达网络 - 则DateTime.ToString(QUOT; MM / DD / YYYY HH:MM:SS.FFF")导致类似" 2013年9月14日07.20.31.371"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WP8的应用程序,这将当前时间发送到Web服务。



我通过调用则DateTime.ToString(MM / DD获得日期时间字符串/ YYYY HH:MM:ss.fff格式)。对于大多数用户来说,它的伟大工程,给了我正确的字符串,如2013年9月10日04:04:31.415。但对于一些用户造成字符串是像2013年9月14日07.20.31.371,这导致我​​的web服务的问题。



是因为一些文化的格式问题?我怎样才能确保结果字符串是由结肠,而不是点分隔的?


解决方案

它是因为有些文化的格式问题?




是的。您的用户必须是一种文化,时间分隔符是一个点。两者:和/被解释在文化敏感的方式定制的日期和时间格式




我怎样才能确保结果字符串是由结肠,而不是点?


分隔< /块引用>

我建议指定 CultureInfo.InvariantCulture

 字符串文本=则DateTime.ToString(MM / DD / YYYY HH:MM:SS.FFF,
CultureInfo.InvariantCulture);



另外,你的可能的只是报价的时间和日期分隔符:

 字符串文本=则DateTime.ToString(MM'/'DD'/'YYYY HH:毫米:SS.FFF ); 



...但是,这将使你有趣的结果,你可能不会想到,如果你获得一种文化,默认日历系统不是公历上运行的用户。例如,以下面的代码:

 使用系统; 
使用System.Globalization;
使用的System.Threading;

类测试
{
静态无效的主要()
{
现在的DateTime = DateTime.Now;
CultureInfo的文化=新的CultureInfo(AR-SA); //沙特阿拉伯
Thread.CurrentThread.CurrentCulture =文化;
Console.WriteLine(now.ToString(YYYY-MM-DDTHH:MM:ss.fff格式));
}
}

这产生输出(9月18日2013年)的:

  1434年11月12日15:04:31.750 

我的猜测是,您的Web服务将通过感到惊讶!



其实我建议不要只使用不变的文化,而的更改为ISO-8601日期格式:

 字符串文本=日期时间。的ToString(YYYY-MM-DDTHH:MM:ss.fff格式); 

这是一个更加全球公认的格式 - 这也是排序,使月和日订单明显。 (尽管2013年6月7日可以解释为根据读者的文化6月7日和7月6日)。


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

I get the datetime string by calling DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff"). 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?

I'd suggest specifying 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"));
    }
} 

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!

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

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(QUOT; MM / DD / YYYY HH:MM:SS.FFF&QUOT;)导致类似&QUOT; 2013年9月14日07.20.31.371&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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