Link to home
Start Free TrialLog in
Avatar of PedroG
PedroG

asked on

Date format and CDate!!!

I have a major problem with date!!!

My user inserts the date in a predefined Date format. My problem consists in converting this value to a date format.

I’m using CDate, but I’ve found out that this function is local dependent. The value of the result could be different according to the user regional settings.

I want to know what is the system short date format. I’ve found out that it is stored in the registry at HKey_Current_USER\Control Panel\International\sShortDate, but I also discovered that this key could not exist, in witch case the format is determined by the geographic location. Is there any API to find out what is the format????
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Give this a try:

Private Declare Function GetLocaleInfo _
                Lib "kernel32" _
                Alias "GetLocaleInfoA" _
                (ByVal Locale As Long, _
                 ByVal LCType As Long, _
                 ByVal lpLCData As String, _
                 ByVal cchData As Long _
                ) As Long
Private Declare Function GetSystemDefaultLCID _
                Lib "kernel32" () As Long
Private Const LOCALE_SSHORTDATE = &H1F

Private Sub Form_Load()
Dim lngLCID As Long
Dim lngReturn As Long
Dim strReturn As String

    lngLCID = GetSystemDefaultLCID()
    strReturn = String$(20, 0)
    lngReturn = GetLocaleInfo(lngLCID, LOCALE_SSHORTDATE, strReturn, Len(strReturn))
    MsgBox Left$(strReturn, lngReturn - 1)
End Sub

Avatar of caraf_g
caraf_g

If your user inserts the date in a predetermined date format, it should be easy for you to determine from their input what the year YYYY, month MM and day DD of that date should be.

Once you've got this information you can create a string of the form YYYY/MM/DD

Then use CDate on that string to convert to a VB Date format.

Because you've made up the string as YYYY/MM/DD, the CDate function can ONLY interpret the string as a date in YYYY/MM/DD format and therefore will give you the correct result regardless of the locale settings on your system.

Your problem was that you were formatting the string as DD/MM/YYYY, which in certain locales and for certain values can be interpreted as a string in format MM/DD/YYYY

For instance, if you pass string "05/06/1999" into the CDate function, depending on locale, it will either interpret it as 5 June 1999 or as May 6, 1999

But if you pass in the string "1999/06/05" into CDate, it can only interpret it as YYYY/MM/DD and hence as 5 June 1999.

Good luck
You can use CDate to get date in your textbox. The only situation when CDate won't work is when user's system date format has "." as last character in format (e.g. "dd.mm.yyyy.")

2 solutions:
1. Say to your users:
"Do not write the last "." in textbox or modify your system date format"
2. Programmatically check for last "."
   Dim sDate as string
   sDate = txtDate.Text
   if right$(sDate, 1) = "." Then
      sDate = left$(sDate, len(sDate)-1)
   endif
Then use cDate function.
Avatar of PedroG

ASKER

caraf_g post your answer, cos your's is the best way to do it, and it do work
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial