Dates And Times

Kurt

Regular
Joined
Feb 14, 2003
Messages
99
Location
Copenhagen
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
 
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?
Visual Basic:
        Dim myDateFormat As New System.Globalization.DateTimeFormatInfo()
        Dim dtTest As New Date()
        myDateFormat.FullDateTimePattern = "yyMMddHHmmss"
        str = "030215141623"
        dtTest = System.DateTime.Parse(str, myDateFormat )
 
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.
 
Personally I'd use international date format, i.e. yyyy-mm-dd. That tends to be interpreted correctly by most things.
 
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;
Visual Basic:
    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 wasn't the intention, here's how should look

Visual Basic:
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