|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Date and regional settings problemI have searched for this solution on the net, but cannot find any.
Using VB6. My application reads a date from a text file in the format "dd.mm.yyyy" If the regional settings is set to for example Polish, Now() will return: 2009-06-30 How can I convert my dd.mm.yyyy into the correct format based on the regional format so that the date functions like IsDate will work ? David
Show quote
Hide quote
"David DB" <er_forts***@hotmail.com> wrote in message If I'm not mistaken, VB will NEVER misinterpret the date format of news:OnlcXYa%23JHA.200@TK2MSFTNGP05.phx.gbl... >I have searched for this solution on the net, but cannot find any. > > Using VB6. > > My application reads a date from a text file in the format "dd.mm.yyyy" > > If the regional settings is set to for example Polish, Now() will return: > 2009-06-30 > > How can I convert my dd.mm.yyyy into the correct format based on the > regional format so that the date functions like IsDate will work ? yyyy-mm-dd. so if you parse the text and re-arrange it to that format before passing it to IsDate(), you should be fine. I wish the entire planet would just use yyyy-mm-dd. Europeans and Americans can argue dd/mm vs. mm/dd all day and they're BOTH wrong. Only the ISO format really makes sense. On Tue, 30 Jun 2009 13:53:01 -0400, "Jeff Johnson" <i.get@enough.spam> "Also, it makes sorting transparent" - JDS, on 2009-07-02 09:43:16wrote: >I wish the entire planet would just use yyyy-mm-dd. Europeans and Americans >can argue dd/mm vs. mm/dd all day and they're BOTH wrong. Only the ISO >format really makes sense. :) J.Jeremiah D. Seitz Omega Techware http://www.omegatechware.net Bob Riemersma recently posted a nice function for this on
StackOverflow http://stackoverflow.com/questions/1059930/a-better-cdate-for-vb6/1061520#1061520 David DB wrote:
> How can I convert my dd.mm.yyyy into the correct format based on the Strings will always be problematic, although as Herve suggests this is probably the > regional format so that the date functions like IsDate will work ? ISO format is probably least so. Not that that's what you have here, but I digress. Anyway, the best answer is to convert them to Date variables, which are totally unambiguous. TheDate = DateSerial(CInt(Right$(d$, 4)), CInt(Left$(d$, 2)), CInt(Mid$(d$, 4, 2))) Where d$ is your date in the format you ask of. "Karl E. Peterson" <k***@exmvps.org> wrote in message HAHAHAHAHAHAHAHAHAHAHAHAHA!!!news:ukP3yIb%23JHA.2824@TK2MSFTNGP03.phx.gbl... > Strings will always be problematic, although as Herve suggests this is > probably the ISO format is probably least so. Not that that's what you > have here, but I digress. De plane! De plane! Jeff Johnson wrote:
> "Karl E. Peterson" <k***@exmvps.org> wrote in message Heh, couldn't help myself... :-)> news:ukP3yIb%23JHA.2824@TK2MSFTNGP03.phx.gbl... > >> Strings will always be problematic, although as Herve suggests this is >> probably the ISO format is probably least so. Not that that's what you >> have here, but I digress. > > HAHAHAHAHAHAHAHAHAHAHAHAHA!!! > > De plane! De plane! Here is a function converter from any date format, to a Date variable. This
is similar to what Karl posted, but works if the month or day is a single digit without a leading 0. Option Explicit ' YY below also means YYYY in Windows 98/2000+. See DateSerial() help. Public Enum EnumAnyDateFormat anyDateFmtDDMMYY anyDateFmtMMDDYY anyDateFmtYYMMDD End Enum Private Sub Form_Load() Dim d As Date d = ConvertAnyDateFormat("02-01-09", "-", anyDateFmtDDMMYY) Debug.Print d ' In Control Panel format End Sub Public Function ConvertAnyDateFormat(ByVal sDate As String, _ ByRef sSeparatorChar As String, _ ByVal DateFormat As EnumAnyDateFormat) As Date Dim sDateParts sDateParts = Split(sDate, sSeparatorChar) If UBound(sDateParts) = 2 Then ' 3 parts found, looks like a date Select Case DateFormat Case anyDateFmtYYMMDD ConvertAnyDateFormat = DateSerial(CInt(sDateParts(0)), _ CInt(sDateParts(1)), CInt(sDateParts(2))) Case anyDateFmtMMDDYY ConvertAnyDateFormat = DateSerial(CInt(sDateParts(2)), _ CInt(sDateParts(0)), CInt(sDateParts(1))) Case anyDateFmtDDMMYY ConvertAnyDateFormat = DateSerial(CInt(sDateParts(2)), _ CInt(sDateParts(1)), CInt(sDateParts(0))) End Select End If End Function Nobody wrote:
> Here is a function converter from any date format, to a Date variable. This Yeah, that's a good natural progression. Then, you could do a little test where if > is similar to what Karl posted, but works if the month or day is a single > digit without a leading 0. the year is <100, add 1900 for 40-99 or add 2000 for 0-39, and so on. Possibilities are endless, once you realize you're left to roll yer own. |
|||||||||||||||||||||||