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

6

144

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?

Orazio answered 18/9, 2013 at 13:52 Comment(2)
the only difference is colon vs dot.Orazio
Refer to .Net Standard 2 samplesGardia
C
261

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", CultureInfo.InvariantCulture);

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

Cyclic answered 18/9, 2013 at 13:55 Comment(7)
thanks Jon. I thought the result string would be exactly like the format string. I will try your suggestion.Orazio
Just one small consideration. When loading a CultureInfo object from a specific code I always suggest to set the useUserOverride parameter to false, otherwise some user settings can override the culture settings. For example in your case I suggest to use new CultureInfo("ar-SA", false).Pasturage
@DavideIcardi: Interesting, thanks - although in this case the purpose of using Saudi Arabia was specifically to say why using the invariant culture is probably right. Will bear that in mind for future posts though.Cyclic
Halfway trough this extensive answer, I started wondering: would this be Jon again? Yep.Emigration
How to manage same with Html.TextBoxFor(x=>x.Date,"{0:MM/dd/yyyy}")?Dorwin
@VISHMAY: I'm afraid I don't know how to affect the culture used by HtmlHelper.Cyclic
Shouldn't that last call dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff"); also have CultureInfo.InvariantCulture passed in?Coloquintida
T
13

: 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:

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.

Touched answered 18/9, 2013 at 13:56 Comment(2)
It's not just the date separator being a problem - see my answer for an example of the culture affecting the actual numbers, too...Cyclic
\ doesn't escape it!Slaw
D
9

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

DateTime.ToString("MM/dd/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
Durer answered 18/9, 2013 at 13:56 Comment(0)
I
8

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

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!

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

Idden answered 2/9, 2015 at 8:6 Comment(3)
I would be quite curious to find out what really causes this.Denson
It has been recognized and fixed as a Windows 10 locaization Bug. So a windows update on the client machine will fix the issue. More details are available in this blog post: heikniemi.net/hardcoded/2015/08/…Variable
The blog of Jouni Heikniemi, which I linked to above seems to be broken. Relevant windows updates where the issues have been fixed were listed there: 2015-10-09: KB3093266 – Windows 10 KB3088956 – Windows Server 2012 R2 and Windows 8.1 KB3088955 – Windows Server 2012 and Windows 8 KB3088957 – Windows 7 SP1, Windows Server 2008 SP2, Windows Server 2008 R2 SP1, and Windows Vista SP2 Trying to link to the blog again, this time though archive.org: web.archive.org/web/20161030193739/http://www.heikniemi.net/…Variable
A
4

You can use 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"
Assignor answered 23/2, 2015 at 14:43 Comment(0)
E
0

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");
Engadine answered 21/6, 2017 at 11:45 Comment(1)
This is not how to convert a date to a string. Parsing is the reverse!Meyerhof

© 2022 - 2024 — McMap. All rights reserved.