Dates And Times

Kurt

0
Joined
Feb 14, 2003
Messages
113
Location
Los Angeles
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, its very likely that the string isnt 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 cant be used to asign the correct values to a variable of that type...
Any suggestions are very welcome.

Kurt
 
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.
 
ok, but why is the following not working?
Code:
        Dim myDateFormat As New System.Globalization.DateTimeFormatInfo()
        Dim dtTest As New Date()
        myDateFormat.FullDateTimePattern = "yyMMddHHmmss"
        str = "030215141623"
        dtTest = System.DateTime.Parse(str, myDateFormat )
 
Its 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 its dependent on regional settings.... unless the trick with the mmm format as month always works, like TechnoTone suggests.
 
Personally Id use international date format, i.e. yyyy-mm-dd. That tends to be interpreted correctly by most things.
 
Dont allow the user to input a string date. Use ComboBoxes or a DatePicker instead. These methods make avoiding the problem rather simple.
 
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 cant 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 thats not following the local settings, but invariable, so its 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;
Code:
    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]
 
oops, that wasnt the intention, heres how should look

Code:
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
 
Back
Top