Kurt Posted February 28, 2003 Posted February 28, 2003 Hi, I have a problem with dates and times. A lot of functions exist to convert a date variable to a string in any format one likes, but the other way arround gives more trouble. If a string is build run time and then parsed to a date, it's very likely that the string isn't recognized as a date. And when it is, changing the regional settings of the pc makes probably that an exeption is thrown again. Anyone knows about how to write more globalized functionality? Great properties of the date datatype like Hour/Minute/Year and so on are read only, so they can't be used to asign the correct values to a variable of that type... Any suggestions are very welcome. Kurt Quote qrt
TechnoTone Posted February 28, 2003 Posted February 28, 2003 Whenever I want a date string to be interpretted as a date I specify the month part as mmm (28/Feb/2003 for example). This helps to eliminate any possible confusion. Quote TT (*_*) There are 10 types of people in this world; those that understand binary and those that don't.
Kurt Posted February 28, 2003 Author Posted February 28, 2003 ok, but why is the following not working? Dim myDateFormat As New System.Globalization.DateTimeFormatInfo() Dim dtTest As New Date() myDateFormat.FullDateTimePattern = "yyMMddHHmmss" str = "030215141623" dtTest = System.DateTime.Parse(str, myDateFormat ) Quote qrt
*Gurus* divil Posted February 28, 2003 *Gurus* Posted February 28, 2003 Why on earth would you want to do something like that? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Kurt Posted February 28, 2003 Author Posted February 28, 2003 It's just the case that I get this format from another machine. Of course I can reformat the string to a 'normal' format, but then stil it's dependent on regional settings.... unless the trick with the mmm format as month always works, like TechnoTone suggests. Quote qrt
*Gurus* divil Posted February 28, 2003 *Gurus* Posted February 28, 2003 Personally I'd use international date format, i.e. yyyy-mm-dd. That tends to be interpreted correctly by most things. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
a_jam_sandwich Posted February 28, 2003 Posted February 28, 2003 I always use date RAW for as a double for anything to do with tranfering. Andy Quote Code today gone tomorrow!
*Gurus* Derek Stone Posted February 28, 2003 *Gurus* Posted February 28, 2003 Don't allow the user to input a string date. Use ComboBoxes or a DatePicker instead. These methods make avoiding the problem rather simple. Quote Posting Guidelines
Kurt Posted March 4, 2003 Author Posted March 4, 2003 If a Combobox is populated with date-times from an SQL database, the dates are displayed as strings, using the local settings of the machine. Using the string selected by a user to perform a query in the SQL database can already go wrong when the SQL provider can't convert the string to a date-time value. The best solution I can come up with for the moment is to populate the Combobox with date time strings in a format that's not following the local settings, but invariable, so it's easy to know which parts of the string represent the day, the month, and so on... Practical is also the constructor for the system.datetime class. Herewith I solved the problem above (02-28-2003 03:05 PM) like this; Public Function GetDateValueFromRecievedStringComli(ByVal str As String) As System.DateTime Dim intDays As Integer Dim intMonths As Integer Dim intYears As Integer Dim intHours As Integer Dim intMinutes As Integer Dim intSeconds As Integer intYears = CType(Val("20" & Mid(str, 1, 2)), Integer) intMonths = CType(Val(Mid(str, 3, 2)), Integer) intDays = CType(Val(Mid(str, 5, 2)), Integer) intHours = CType(Val(Mid(str, 7, 2)), Integer) intMinutes = CType(Val(Mid(str, 9, 2)), Integer) intSeconds = CType(Val(Mid(str, 11, 2)), Integer) Return New System.DateTime(intYears, intMonths, intDays, intHours, intMinutes, intSeconds) End Function [\vb] Quote qrt
Kurt Posted March 4, 2003 Author Posted March 4, 2003 oops, that wasn't the intention, here's how should look Public Function GetDateValueFromRecievedStringComli(ByVal str As String) As System.DateTime Dim intDays As Integer Dim intMonths As Integer Dim intYears As Integer Dim intHours As Integer Dim intMinutes As Integer Dim intSeconds As Integer intYears = CType(Val("20" & Mid(str, 1, 2)), Integer) intMonths = CType(Val(Mid(str, 3, 2)), Integer) intDays = CType(Val(Mid(str, 5, 2)), Integer) intHours = CType(Val(Mid(str, 7, 2)), Integer) intMinutes = CType(Val(Mid(str, 9, 2)), Integer) intSeconds = CType(Val(Mid(str, 11, 2)), Integer) Return New System.DateTime(intYears, intMonths, intDays, intHours, intMinutes, intSeconds) End Function Quote qrt
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.