microkarl Posted October 27, 2005 Posted October 27, 2005 Hello all, I have a question about convert strings into dataTime format (in a globalization manner). Consider I have the following code: Dim strMonth As String = "12" Dim strDay As String = "30" Dim strYear As String = "2005" strTime = strMonth & "/" & strDay & "/" & strYear dtmTime = CDate(strTime) dtmTime.ToString("MM/dd/yyyy") The code would work and return "12/30/2005" if we are using United Sates DateTime Format (Regional Settings in Control Panel). However, if we choose some other culture, say Britsh, their regional datetime string format is "DD/mm/yyyy", so the above code would crash because CDate() converts a string to datetime based on the setting from the regional settings. So, my question is that is there any graceful way to handle situation like this easily? I did some research on Google but I found out there's no single function to do it, or maybe I am missing somthing... Please let me know if you guys have any solutions... Thanks, Carl Quote Donald DUCK : YOU ARE FIRED!!!
*Experts* Nerseus Posted October 27, 2005 *Experts* Posted October 27, 2005 My limited experience has shown two issues with dates. Parsing a string and sending a date to a database. When parsing or displaying a string, I like to use the built in functions such as ToShortDateTimeString or other methods of a DateTime variable. I would expect DateTime.Parse () to use the current culture so I usually "don't have to worry about it". On the other hand, when sending things to a database - I use SQL Server mostly - the DB wants a date in one format. From my installs, that's always in the US format (month/day/year) though that may be an option. So, you're line of code "strTime = strMonth & "/" & strDay & "/" & strYear" I would not use unless I was passing that date to SQL server as a string. I wouldn't trust a hard-coded format to work with DateTime.Parse() because of the very reasons you mention. I mention DateTime.Parse but it's more or less the same as CDate, but more .NET oriented than the old VB function. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Administrators PlausiblyDamp Posted October 27, 2005 Administrators Posted October 27, 2005 If you already have the values as 3 seperate numbers you could always use the following way to create a date variable and remove the need to handle formatting. dim d as New DateTime(2005, 12, 30) If you know which regional format the date is coded for you could always use the DateTime.Parse method and specify a location Dim d as DateTime d = DateTime.Parse(s, New System.Globalization.CultureInfo("en-US").DateTimeFormat) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* DiverDan Posted October 27, 2005 *Experts* Posted October 27, 2005 I've run into the same problem except with comma decimal separators instead of dates. What I did is write two functions similar to PlausiblyDamp's post. One function called Store_Value which converts the entered date into my program's format and another called Display_Value which converts a computed date into the user's culture format. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.