C# DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff") 导致类似 "09/14/2013 07.20.31.371"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18874102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:29:08  来源:igfitidea点击:

DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff") resulted in something like "09/14/2013 07.20.31.371"

c#windows-phone-8

提问by Eldorado

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

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

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.

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

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

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

采纳答案by Jon Skeet

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:

我建议指定CultureInfo.InvariantCulture

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

Alternatively, you couldjust 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:

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

11/12/1434 15:04:31.750

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

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

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

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

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

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

回答by MarcinJuraszek

:has special meaning: it is The time separator.(Custom Date and Time Format Strings).

:有特殊含义:它是时间分隔符。自定义日期和时间格式字符串)。

Use \to escape it:

使用\逃脱它:

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

Or use CultureInfo.InvariantCulture:

或使用CultureInfo.InvariantCulture

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

I would suggest going with the second one, because /has special meaning as well (it is The date separator.), so you can have problems with that too.

我建议使用第二个,因为它/也有特殊含义(它是日期分隔符。),所以你也可能会遇到问题。

回答by Jon La Marr

You can use InvariantCulture because your user must be in a culture that uses a dot instead of a colon:

您可以使用 InvariantCulture,因为您的用户必须处于使用点而不是冒号的文化中:

DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);

回答by Cosmin

You can use String.Format:

您可以使用 String.Format:

DateTime d = DateTime.Now;
string str = String.Format("{0:00}/{1:00}/{2:0000} {3:00}:{4:00}:{5:00}.{6:000}", d.Month, d.Day, d.Year, d.Hour, d.Minute, d.Second, d.Millisecond);
// I got this result: "02/23/2015 16:42:38.234"

回答by H?kon Selj?sen

I bumped into this problem lately with Windows 10 from another direction,and found the answer from @JonSkeet very helpful in solving my problem.

我最近从另一个方向使用 Windows 10 遇到了这个问题并发现 @JonSkeet 的答案对解决我的问题非常有帮助。

I also did som further research with a test form and found that when the the current culture was set to "no"or "nb-NO"at runtime (Thread.CurrentThread.CurrentCulture = new CultureInfo("no");), the ToString("yyyy-MM-dd HH:mm:ss") call responded differently in Windows 7 and Windows 10. It returned what I expected in Windows 7 and HH.mm.ss in Windows 10!

我还对测试表单进行了进一步研究,发现当当前区域性设置为"no""nb-NO"在运行时 ( Thread.CurrentThread.CurrentCulture = new CultureInfo("no");) 时,ToString("yyyy-MM-dd HH:mm:ss") 调用在 Windows 7 和Windows 10。它返回了我在 Windows 7 中和 HH.mm.ss 在 Windows 10 中的预期!

I think this is a bit scary! Since I believed that a culture was a culture in any Windows version at least.

我觉得这有点可怕!因为我相信一种文化至少在任何 Windows 版本中都是一种文化。

回答by Hafsal

Convert Date To String

将日期转换为字符串

Use name Space

使用名称空间

using System.Globalization;

Code

代码

string date = DateTime.ParseExact(datetext.Text, "dd-MM-yyyy", CultureInfo.InstalledUICulture).ToString("yyyy-MM-dd");