Home All Groups Group Topic Archive Search About

Date and regional settings problem

Author
30 Jun 2009 5:13 PM
David DB
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 ?

David

Author
30 Jun 2009 5:53 PM
Jeff Johnson
Show quote Hide quote
"David DB" <er_forts***@hotmail.com> wrote in message
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 ?

If I'm not mistaken, VB will NEVER misinterpret the date format of
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.
Author
2 Jul 2009 1:43 PM
Jeremiah D. Seitz
On Tue, 30 Jun 2009 13:53:01 -0400, "Jeff Johnson" <i.get@enough.spam>
wrote:

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

"Also, it makes sorting transparent" - JDS, on 2009-07-02 09:43:16

    :)

    J.
    Jeremiah D. Seitz
    Omega Techware
    http://www.omegatechware.net
Author
2 Jul 2009 4:56 PM
Mark
Bob Riemersma recently posted a nice function for this on
StackOverflow

http://stackoverflow.com/questions/1059930/a-better-cdate-for-vb6/1061520#1061520
Author
30 Jun 2009 6:40 PM
Karl E. Peterson
David DB wrote:
> 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 ?

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.

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.
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
30 Jun 2009 8:19 PM
Jeff Johnson
"Karl E. Peterson" <k***@exmvps.org> wrote in message
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!
Author
30 Jun 2009 8:36 PM
Karl E. Peterson
Jeff Johnson wrote:
> "Karl E. Peterson" <k***@exmvps.org> wrote in message
> 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!

Heh, couldn't help myself...  :-)
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
30 Jun 2009 7:49 PM
Nobody
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
Author
30 Jun 2009 8:38 PM
Karl E. Peterson
Nobody wrote:
> 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.

Yeah, that's a good natural progression.  Then, you could do a little test where if
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.
--
..NET: It's About Trust!
http://vfred.mvps.org