Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date format and CDate!!!

Posted on 1999-01-14
5
Medium Priority
?
1,406 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 210 total points
ID: 1456277
See my comment
0

Featured Post

Independent Software Vendors: 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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

715 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