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