为什么这个DateTime.Parse(字符串)代码有效? [英] Why is this DateTime.Parse(string) code working?

查看:164
本文介绍了为什么这个DateTime.Parse(字符串)代码有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿有社区,

我有一个问题,为什么我的代码工作正常。 :P

是的,听起来很奇怪,但我只是好奇......



当我用英语开始申请时我我正在设置我目前的文化信息:

Hey there community,
I have one question why my code is working. :P
Yeah, sounds strange, but I'm just curious...

When I'm starting my application in English I'm setting my current culture info:

CultureInfo cultureInfo;
cultureInfo = new CultureInfo("en-US");
cultureInfo.DateTimeFormat.ShortDatePattern = "MM/dd/yy";
cultureInfo.DateTimeFormat.LongDatePattern = "MM/dd/yyyy";
cultureInfo.DateTimeFormat.ShortTimePattern = "HH:mm";
cultureInfo.DateTimeFormat.LongTimePattern = "HH:mm:ss";
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo





查看 DateTime.Parse() [ ^ ],我找到以下描述:

使用解析日期和时间字符串当前文化的惯例。 - > Parse(String)重载



但是下面的Parse(使用我们的奥地利日期格式)可以正常工作:



Looking in the MSDN of DateTime.Parse() [^], I find the following description:
"Parse a date and time string by using the conventions of the current culture. --> Parse(String) overload"

But the following Parse (with our Austrian date format) works without problems:

DateTime toCompare = DateTime.Parse("01.01.1900 00:00:00");





任何人都可以解释为什么我不必使用DateTime.Parse( string,IFormatProvider)重载?

据我了解MSDN,我当前的.Parse()不应该工作...



谢谢,马库斯



Can anyone explain why i don't have to use the DateTime.Parse(string, IFormatProvider) overload?
As far as I understand the MSDN, my current .Parse() should not work...

Thanks, Markus

推荐答案

正斜杠/并不是字面意义上的正斜杠 - 它代表日期格式中使用的分隔符。你没有改变它(你可以用 cultureInfo.DateTimeFormat.DateSeparator =/; 这样做,因此点。仍然可以作为分隔符。顺便说一句,第一个01成为一月,第二个01成为那个月的第一天......
The forward slash "/" is not meant to be a forward slash literally - it stands for the separator used in the date format. You did not change that (you could do so with cultureInfo.DateTimeFormat.DateSeparator = "/";, consequently the dot "." is still acceptable as the separator. And by the way, the first 01 became January, the second 01 the first day of that month...


这篇关于为什么这个DateTime.Parse(字符串)代码有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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