Changing Date time Format
Dear All,
Hope all CodeGuru Members are fine.
Is there any way by which i can change the Short Date & Long Date style as defined in the "Regional Settings" Tab Date/Time from within VB6.
1st i need to know what is the present format then if needed change it and then again set it back to the setting that was assigned previously.
Is there any API to do this. If there is how to do it.
Regards
Amarjit
Re: Changing Date time Format
why do you need to mess with your user's settings?
Re: Changing Date time Format
Depending on the OS, perhaps it's a registry setting?
...m
Re: Changing Date time Format
Actually i have an access database where i search the date field as yyyy-mm-dd. Whenever the short date style in Regional Setting is not set to "yyyy-mm-dd" it generates wrong report due to swapping of the month and date. Also i check whether the date inputed has a length 10 or not.
Is there any way to set the search in access as yyyy-mm-dd. So that whetever may be the system date my search will be on the format yyyy-mm-dd
Any help on the above topic
I am using win 98
Regards
Amarjit
Re: Changing Date time Format
just format the date as you would like before performing the search.
Code:
Dim sDate As String
sDate = Format$(Now, "yyyy-mm-dd")
SQL = "SELECT .......... WHERE whatever = " & sDate
Re: Changing Date time Format
have you tested this process before writing.
I have tested this process but it give wrong reasult as date and month part get swapped for date & month. Sometimes it do not return reasult when i give a date range that means from start date to end date.
are you sure or just giving me an answer to my query.
regards
Amarjit
Re: Changing Date time Format
eh?
system locale settings dictate how a Date is converted to a String and visa-versa. Assuming that your users are inputting the Dates, they will use their local format (which the Format$ function will interpret for you, and change into the format you suggest). You could ask them to place the date in a specified format, or use a MaskEditBox or a DatePicker etc.
Also you may want to use Date literals in your SQL queries, i.e.:
Code:
SQL = "SELECT something FROM something WHERE dDate >= #" & Format$(dDate, "mm/dd/yyyy") & "#"
(place the Date in mm/dd/yyyy format and surround it with #)
The system affects the program, not the other way round. The examples I've posted may not be exactly what you need, but then you haven't explained or given any examples of what your doing other that saying that you need the date in yyyy-mm-dd format, which the Format$ function will do for you.
Re: Changing Date time Format
Maybe the next could help ...
I had the same problem when I use VisualBasic 6 spanish version installed in a Windows 95/98 english version
Re: Changing Date time Format
Quote:
SQL = "SELECT something FROM something WHERE dDate >= #" & Format$(dDate, "mm/dd/yyyy") & "#"
Format mm/dd/yyy is the Syntax of access to match date,
don't take care to Regional Settings on this.
Re: Changing Date time Format
Or, store Date fields as Strings. Just build them correctly at run-time for each region.
Re: Changing Date time Format
I can understand what all members has to say.
can anyone tell me if i need to compare date range then i pass an SQL string from VB with the Format Command.
There is also a search engine present inside Access so that it can compare the date based on user input and give us the resultant rows. Then the date fromat user by access to search rows based on my criteria depends on the local settings of the computer. If the date format of local settings do not match the date settings specially in case of month & date then the resultant rows returned may be wrong.
The problem lies in month & day part of the date not with the century part that may be in YY or YYYY format.
How will the access inbuilt SQL search engine know 02-03-2006 = 02nd March 2006 or 03rd February 2006. This depends on the local settings.
If we fix the locale settings to yyyy-mm-dd then it search for all dates on the basis yyyy-mm-dd on users input.
Hope i am able to make the whole sinerio much more clear.
Any more suggestions.
Regards
Amarjit
Re: Changing Date time Format
A Time-Date field is just a number that is converted into a local Date and Time format. It is stored internally but displayed according to settings. If you send me a DB, then the dates would appear correct even though Regional Settings may differ.
Re: Changing Date time Format
I have got this problem when working with Vb & Crystal reports.
Whenever i send an sql to Crystal Reports the date settings create a problem if it do not match as per the local settings.
We have deviated from the question i asked. Can any one tell me if it is possible to change the date settings in the "Regional Settings" thru API.
Regards
Amarjit
Re: Changing Date time Format
Pop up a msgbox asking the user to change his settings. (Most won't, however)
Why would you want to do that to someone? You'd mess up all his programs that depended on his old settings. And, don't say you'd change them back. Your app would have to run exclusively!
What if the employer depended on the user not being able to change settings, and your app does it?
Think up a better solution.
Re: Changing Date time Format
Ok i am thinking of a better solution.
But can you tell me how the same can be done. Is it wrong to get the required knowledge.
You have told me the pros & cons of the same.
Please tell me how is it possible if you know
Amarjit
Re: Changing Date time Format
Take a look for ScriptoMatic2 which is a free app by the MS Guys. It writes scripts to do Windows Level things. You may be able to alter it that way, or possibly in the registry. I think you'd have to re-start the machine for them to take effect, though.
Re: Changing Date time Format
Amarjit : Based on your PM.
This was found on http://www.a1vbcode.com/app-2790.asp.
You're gonna need the following APIs :
GetSystemDefaultLCID
GetLocaleInfo
SetLocaleInfo
EnumDateFormats
PostMessage
CopyMemory
So, let's put all these (alongwith the associated Constants - and the functions using these APIs) in to a module :
Code:
Option Explicit
Public Const LOCALE_SLANGUAGE As Long = &H2
Public Const LOCALE_SSHORTDATE As Long = &H1F
Public Const DATE_LONGDATE As Long = &H2
Public Const DATE_SHORTDATE As Long = &H1
Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_SETTINGCHANGE As Long = &H1A
Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Declare Function EnumDateFormats Lib "kernel32" _
Alias "EnumDateFormatsA" _
(ByVal lpDateFmtEnumProc As Long, _
ByVal Locale As Long, _
ByVal dwFlags As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Public 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
Public Declare Function SetLocaleInfo Lib "kernel32" _
Alias "SetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String) As Long
Public Function fGetUserLocaleInfo(ByVal lLocaleID As Long, _
ByVal lLCType As Long) As String
Dim sReturn As String
Dim lReturn As Long
lReturn = GetLocaleInfo(lLocaleID, lLCType, sReturn, Len(sReturn))
If lReturn Then
sReturn = Space$(lReturn)
If lReturn Then
fGetUserLocaleInfo = Left$(sReturn, lReturn - 1)
End If
End If
End Function
Public Function theEnumDates(lDateFormatString As Long) As Long
theEnumDates = 1
End Function
Private Function GetStrFromPointer(sString As Long) As String
Dim lPos As Long
Dim sBuffer As String
sBuffer = Space$(128)
Call CopyMemory(ByVal sBuffer, sString, ByVal Len(sBuffer))
lPos = InStr(sBuffer, Chr$(0))
If lPos Then
GetStrFromPointer = Left$(sBuffer, lPos - 1)
End If
End Function
Now, add a button to your form and type this :
Code:
Private Sub Command1_Click()
Dim xCID As Long
Dim xChangedFormat As String
'
xCID = GetSystemDefaultLCID()
xChangedFormat = "MM/dd/yyyy" 'specify date format here!!!
'
If xChangedFormat <> "" Then
Call SetLocaleInfo(xCID, LOCALE_SSHORTDATE, xChangedFormat)
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)
Call EnumDateFormats(AddressOf theEnumDates, xCID, DATE_SHORTDATE)
MsgBox "Date Changed to MM/dd/yyyy format"
End If
End Sub
Try this, I honestly haven't tested this thoroughly, but it should work :)
Another much simpler option may be to run the regional Settings applet from within your program. basically, it will open the Regional settings dialog box, and let the user change the date formats and stuff. Add this to a button click :
Code:
Private Sub Command2_Click()
Call Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl")
End Sub
I hope this helps a bit! :)
Re: Changing Date time Format
Thanks Hannes for that answer. I will reply you after testing it.
Meanwhile another answer bugs me regarding using dates in the access query.
Most of the members have told me not to change the user settigs of the date. As the same may effect them somehow. Can you give me any possible solution regarding using of date ranges in SQL search.
Is it possible to know without the use of any API what is the present date format (long & Short) of the system.
Actually i am bumping into the problem when i fire a query into access 2000 without giving any attention to the date format of the system. If the system date is set to mm/dd/yyyy then the date such as 12/31/2006 is perfectly interpreted whereas 05/06/2006 (5th June 2006) as 5th May 2006. Hence the results returned is not correct.
I know many may have bumped into this problem. I think there may be a possible solution regarding using date formats without depending on the system date format set in the regional settings.
Any further help....
Thanks always
regards
Amarjit
Re: Changing Date time Format
hi,
just wanna try to help, i dont know whether this can give you any solution
what if you try to use the DatePart Function, so that whatever the format of the date, the datepart always give u the right answer.
ex :
MM/DD/YYYY => 10/12/2006
DD/MM/YYYY=> 12/10/2006
Code:
dim d,m, y as string
dim dates
dates = "12/10/2006"
d = datepart("d",dates)
m = datepart("m", dates)
y = datepart ("y", dates)
hope that u got what i mean.
rgrds,
Ecko
Re: Changing Date time Format
Thank for the help.
But my question is how will i know the present format of the date whether it is mm/dd/yyyy or dd/mm/yy or yyyy/dd/mm etc ......
Is there any way too know that
Regards
Amarjit
Re: Changing Date time Format
I hoe it's useful
Code:
Private Const LOCALE_SSHORTDATE As Long = &H1F
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 GetUserDefaultLCID Lib "kernel32" () As Long
Private Function GetDateFormat() As String
Dim Val_x As Long
Dim Simbolo As String
Dim r1 As Long
Dim r2 As Long
Dim p As Integer
Dim Locale As Long
Val_x = LOCALE_SSHORTDATE
Locale = GetUserDefaultLCID()
r1 = GetLocaleInfo(Locale, Val_x, vbNullString, 0)
Simbolo = String$(r1, 0)
r2 = GetLocaleInfo(Locale, Val_x, Simbolo, r1)
p = InStr(Simbolo, Chr$(0))
If p > 0 Then
GetDateFormat = Left$(Simbolo, p - 1)
End If
End Function
test
Code:
Private Sub Command_Click()
Dim Sd() As String
Dim DateFormat As String
DateFormat = GetDateFormat
Sd = Split(DateFormat, "/")
Debug.Print DateFormat
Debug.Print Sd(0)
Debug.Print Sd(1)
Debug.Print Sd(2)
End Sub
1 Attachment(s)
Re: Changing Date time Format
Hanesa,
I got the VB6 code from this link, its the same as you have posted
http://www.a1vbcode.com/app-2790.asp
and here with i attached the project which is converted from VB6 to VB.Net2005. Kindly check this. Totally it shows 6 errors when i Build the project.
the following error repeats.
Error 1 'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type.
Error 4 'As Any' is not supported in 'Declare' statements.
i changed the Any to Object its ok fine. But the AddressOf is not working. I don't know how to change it as Delegate. Can you pls modify and send me the attached project.
Thanks for your support.
Thanks and Regards,
V.Chock.
Re: Changing Date time Format
Quote:
Originally Posted by itchock
Are you sure ¿ Then I should have mentioned it there ( I mentioned it there now ). I did something similar in any case, so there must have been a confusion somewhere. Sorry about that, it's difficult if you have hundreds of stuff.
Quote:
Originally Posted by itchock
and here with i attached the project which is converted from VB6 to VB.Net2005. Kindly check this. Totally it shows 6 errors when i Build the project.
Don't get me wrong, but for the 100th time people, it is not good to use the "upgrade wizard"! Mainly because of these type of errors. IMHO, it is best to redo the entire applicaton, or even try an incremental update.
Any is not supported in .NET. AddressOf works a bit differently in .NET
The proper definitions for those APIs ( for .NET ) is the following :
Code:
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Private Declare Function EnumDateFormats Lib "KERNEL32.dll" Alias "EnumDateFormatsA" (ByVal lpDateFmtEnumProc As Int32, ByVal Locale As Int32, ByVal dwFlags As Int32) As Int32
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Int32, ByRef Source As Any, ByVal Length As Int32)
Private Declare Function GetSystemDefaultLCID Lib "kernel32.dll" () As Int32
Private Declare Function SetLocaleInfo Lib "kernel32.dll" Alias "SetLocaleInfoA" (ByVal Locale As Int32, ByVal LCType As Int32, ByVal lpLCData As String) As Int32
Private Declare Function GetLocaleInfo Lib "kernel32.dll" Alias "GetLocaleInfoA" (ByVal Locale As Int32, ByVal LCType As Int32, ByVal lpLCData As String, ByVal cchData As Int32) As Int32
Have a look at this article explaining the differences between API in VB 6 and .NET :
http://www.codeguru.com/vb/gen/vb_ge...le.php/c11981/
make sure you download the API Viewer 2004 ( for the proper .NET API declarations ) :
http://www.activevb.de/rubriken/apiv...viewereng.html
A better way ( the actual way to use unmanaged code in .NET ) is to use the System.Runtime.InterOpServices namespace :
http://msdn2.microsoft.com/en-us/lib...pservices.aspx
Here's more info on delegates in .NET :
http://msdn2.microsoft.com/en-us/lib...22(VS.80).aspx
And here's more info on localisation in .NET :
http://www.c-sharpcorner.com/UploadF...et_CSharp.aspx
Remember, this is the VB 6 forum. If you have VB.NET questions, visit the VB.NET forum :
http://www.codeguru.com/forum/forumdisplay.php?f=12
Now, please, let me get the rest my mind deserves ( I don't have "Taking A Break" in my signature for nothing. )