windows 如何将日期从德语格式转换为英语格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7468789/
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
How to convert date from german format to english format?
提问by kampi
I'm making an application which runs on every computer in my company, and queries information from the current logged on user from Active Directory. Unfortunately we use German and English operating systems too, and this is the reason why i'm having the following problem. On an english OS the queried date format is YYYY.MM.DD, but on a German Os the format is DD.MM.YYYY. So my question is, how can i convert a german date to an english date foramt? This is important because i will insert this date into an SQL database, but on german PC's i get the following error: "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value." I don't know if this is relevant or not, but i have the dates in a char * variable.
我正在制作一个在我公司的每台计算机上运行的应用程序,并从 Active Directory 中查询当前登录用户的信息。不幸的是,我们也使用德语和英语操作系统,这就是我遇到以下问题的原因。在英语操作系统上,查询日期格式是 YYYY.MM.DD,但在德语操作系统上,格式是 DD.MM.YYYY。所以我的问题是,如何将德国日期转换为英文日期格式?这很重要,因为我会将这个日期插入到 SQL 数据库中,但是在德国 PC 上,我收到以下错误:“将 varchar 数据类型转换为日期时间数据类型导致值超出范围。” 我不知道这是否相关,但我在 char * 变量中有日期。
Thanks!
谢谢!
回答by kampi
I found a much easier way to do this in SQL.
我在 SQL 中找到了一种更简单的方法来做到这一点。
SELECT convert(datetime, '19.08.2011 13:17:01', 104) This will result the following: 2011-08-19 13:17:01.000 I think this is the easiest way to do what i want. Thanks for your help guys!
SELECT convert(datetime, '19.08.2011 13:17:01', 104) 这将导致以下结果: 2011-08-19 13:17:01.000 我认为这是做我想做的最简单的方法。谢谢你们的帮助!
回答by Yahia
IF you are ABSOLUTELY sure that you always have the above german format then I see a C option by creating a same-length char* for the English representation and shuffling the values around:
如果您绝对确定您始终具有上述德语格式,那么我会通过为英文表示创建相同长度的 char* 并调整周围的值来看到 C 选项:
MyEnglishD [0] = MyGermanD [ 6 ];
MyEnglishD [1] = MyGermanD [ 7 ];
MyEnglishD [2] = MyGermanD [ 8 ];
MyEnglishD [3] = MyGermanD [ 9 ];
MyEnglishD [4] = '.';
MyEnglishD [5] = MyGermanD [ 3 ];
MyEnglishD [6] = MyGermanD [ 4 ];
MyEnglishD [7] = '.';
MyEnglishD [8] = MyGermanD [ 0 ];
MyEnglishD [9] = MyGermanD [ 1 ];
Another option which I would prefer would be to use a date format string in you SQL INSERT code (example for Oracle):
我更喜欢的另一个选项是在 SQL INSERT 代码中使用日期格式字符串(例如 Oracle):
INSERT INTO MyTable (MyDateCol,...)
SELECT TO_DATE ( :MyDateVal, :MyDateFormat), :anotherVal... FROM DUAL;
For an english date the format should be 'YYYY.MM.DD' and for the german 'DD.MM.YYYY' .
对于英文日期,格式应为 'YYYY.MM.DD' ,而对于德语为 'DD.MM.YYYY' 。
回答by Louis
You must convert your varchar dates in a datetime format befor writing them.
您必须在写入之前将 varchar 日期转换为日期时间格式。
You can detect "german" format by catching the error and then build a "YYYY-MM-DD" out of the "DD-MM-YYYY" in a separate variable.
您可以通过捕获错误来检测“德语”格式,然后在单独的变量中从“DD-MM-YYYY”中构建“YYYY-MM-DD”。
回答by test30
how about storing data in timestamp? or look for setLocale in your api and set it to e.g. en-us
如何将数据存储在时间戳中?或在您的 api 中查找 setLocale 并将其设置为例如 en-us