Solved

Date format and CDate!!!

Posted on 1999-01-14
5
1,370 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
5 Comments
 
LVL 69

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now