-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DateTime.TryParse produces different results in different environments #25120
Comments
@Doff3n what are the configurations of your host and VM? Both the same Windows version? |
Localhost/my PC: VM: |
I am not sure what configurations to set here. |
@Doff3n on your VM, are you sure TryParse returned true? would be nice too if you can get the output of the following lines on the 2 machines foreach (string s in new CultureInfo("nb-NO", false).DateTimeFormat.GetAllDateTimePatterns())
{
Console.WriteLine(s);
} |
I wrote a console app and built it on the VM, this produces the following different results
Copied output localhost: Copied output VM: |
.Net core runtime 1.0 is also installed on the VM, but the console app has |
@Doff3n here is what I think happened: on the VM, TryParse failed (returned false and not true). which means the parsing is failed and you cannot depend on the return datetime. The reason it failed is that on the VM, the default time pattern HH.mm while on the other machine it is HH:mm. so when parsing "29.01", the parser cannot know if to parse it as a date or parse as time because the date and the time separator are same ".". your code should always use ParseExact to ensure the result. something like var result: DateTime.TryParseExact("29.01", "dd.MM", new CultureInfo("nb-NO", false), DateTimeStyles.None, out var parsedDateTime) Please, lat's know if this is not the case. |
We are implementing a simple string filter, so we were expecting not to know the date format, but support a range of formats |
@tarekgh is this something we need to fix for 2.1? |
It looks like the VM only has the HH.mm format in Windows, while my PC has HH:mm in addition to HH.mm |
Expecting DateTime parsing to succeed with any string is the wrong assumption. the guidelines for parsing is either you pass the format pattern used with the date formatting or use invariant culture all the time for formatting and parsing. please try to fix your design according to that or handle the parsing failure when it happens.
Globalization data can change between the different OS versions. so this is expected. in summary, this is by design from the framework side. |
@Doff3n commented on Wed Feb 14 2018
DateTime.TryParse produces different results in different environments
General
I am trying to parse a date, with DateTime.TryParse and with a provided Norwegian culture and no DatetimeStyles:
I am purposely sending in "29.01" and expecting the result to parse to a default Datetime: "29.01.2018",
I also expect that 29.01.18 and 29.01.2018 parses to the correct dates.
In norway the date format is dd.MM.YYYY HH:mm
Code:
https://github.com/Doff3n/HelloDate
Commands:
Running this on my PC:
parsedDateTime: 29.01.2018 00:00:00
result: True
Running this on the VM:
parsedDateTime: 01.01.0001 00.00.00
result: False
.Net core sdk version: 2.1.4, runtime: 2.0.5
In desperation I have also tried setting Date and Time formats in Windows, but I would have expected the property UseUserOverride: false to not allow user settings.
The text was updated successfully, but these errors were encountered: