Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

qrt
Posted
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.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted

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 )

qrt
Posted
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.
qrt
Posted

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]

qrt
Posted

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

qrt

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...