Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1420
  • Last Modified:

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????
1 Solution
Éric MoreauSenior .Net ConsultantCommented:
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 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

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)
Then use cDate function.
PedroGAuthor Commented:
caraf_g post your answer, cos your's is the best way to do it, and it do work
See my comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now