开发者

How to convert date from german format to english format?

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 开发者_StackOverflow社区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.

Thanks!


I found a much easier way to do this in 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!


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:

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

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


You must convert your varchar dates in a datetime format befor writing them.

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.


how about storing data in timestamp? or look for setLocale in your api and set it to e.g. en-us

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜