Solved

Date format and CDate!!!

Posted on 1999-01-14
5
1,392 Views
Last Modified: 2008-03-17
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????
0
Comment
Question by:PedroG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 1456273
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

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456274
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
0
 
LVL 15

Expert Comment

by:ameba
ID: 1456275
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.
0
 
LVL 2

Author Comment

by:PedroG
ID: 1456276
caraf_g post your answer, cos your's is the best way to do it, and it do work
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 70 total points
ID: 1456277
See my comment
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Added a column screws up code 5 78
VBA: copy range dynamically based on config sheet v2 3 61
Outlook Macro to delete emails not meeting keywords 56 116
VB6 Enviroment 3 37
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question