-
Hi. I've written an app for use in the UK and Netherlands (no, it's not where Peter Pan lives) and it's very dependant on the date format being UK (dd/mm/yy) and NOT US (mm/dd/yy), but some <CENSORED> in the Netherlands keeps changing the date format back to US, which rather knackers things.
Is there any way I can:
a) Check what the system date format is
b) Change the system date format to UK if necessary
If you're wondering why the date format is so crucial, it's because our customer splits the year up into four quarters, and thirteen "weeks" within each quarter and so each record we capture has to be assigned a conglomerate of this quarter/week thing, and, as we found this week, if IT go in and change your regional settings while you're away on hols, when you come back you find that records that should be assigned to Q2W06 end up in Q2W13. There - all you insomniacs cured now? And can any bright sparks out there tell me who the customer is? har har har
-
Not the best but I once used this in an application to ensure the day and month were the 'right' way round; (speaking from a UK point of view! :) )
Code:
If Left(Cstr(Format("15/1","Short Date")),2)<>"15" Then
Msgbox "Please change your date format to DD/MM/YYYY"
.. do stuff ...
End If
-
Display vs Storage
There is another solution, which is a bit of a compromise.
Differentiate between stored dates and displayed dates. Store all dates in Date variables, and let the user display them in any way they want to.
VB dates are stores as a double precision floating point number, and represents the number of days since January 1900, so it's not tied down to any format until it needs to be displayed.
At any time you can get Format("dd-mm-yyyy", MyDate) from your variable while the user displays Format("mm/dd/yyyy", MyDate).
I have used some American programs that try to force me to use American date formats, but I don't use them for very long. Your user's may not like you prescribing your format to them either.
Shrog
-
Good ideas chaps, but I was sort of hoping for a registry thing or something. Dunno why really, since both your methods would work fine I reckon. THANKS!!
Shrog, it's not the users that mess with the formats, it's their flaming IT department that are the problem. Anyway, why you standing up for users, you know they are the only thing that comes between us programmers and world domination...
-
yeah
Sorry, I lost it there for a minute. Don't know what I was thinking...
Shrog
-
Well, I'm from the Netherlands, and work at an IT department. And if some app would change our systems date format, I would delete it as soon as I found out. Anyway, these settings are set each time our users log in, so any change would be temporary.
What I mean to say is, that (if possible) you should design your application to run with all regional settings possible.
But, who am I to tell you what to do.
so here is the code:
Code:
Option Explicit
Private Const LOCALE_SYSTEM_DEFAULT = &H800
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Const LOCALE_SLONGDATE = &H20 ' long date format stringPrivate Const LOCALE_USER_DEFAULT = &H400
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 SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long
Private Sub Command1_Click()
Dim sLocale As String
Dim retVal As Long
sLocale = Space(255)
retVal = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sLocale, Len(sLocale))
sLocale = Left(sLocale, retVal)
MsgBox "Short Date Format = : " & sLocale
sLocale = "dd-MM-yyyy"
retVal = SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sLocale)
sLocale = Space(255)
retVal = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sLocale, Len(sLocale))
sLocale = Left(sLocale, retVal)
MsgBox "Short Date Format = : " & sLocale
End Sub
[Edited by Frans C on 11-14-2000 at 01:29 PM]
-
This is best
Don't bother trying to find out the date format, just look at it the way you want:
ukdate=format(now,"dd/mm/yy")
Done
You can change the slashes to dashes and you could change yy to yyyy to get 2000 instead of 00.
Matthew
-
Thanks all, but here's the actual problem I'm having. The user keys a date in bits into three different text boxes, one each for day, month and year. I then compile these three and pass them to a function (which just returns a string) thus:
Code:
TempBatch = SunFiscal(Format(txtFPODDay.Text & "/" & _
txtFPODMonth.Text & "/" & _
txtFPODYear.Text, "dd/mm/yy"))
Trouble is, if the system short date is set as dd/mm/yy then the function receives the date correctly, but if the system short date is reversed (US styley) as mm/dd/yy, then the function receives the date backwards too, and thus the function returns a pile of poo too.
What am I doing wrong here? These bloody format things always do my head in...
-
just ask the user to put the date in the format dd/mm/yy
-
Serial
No, the user enters it correctly. The parts of the date from the user is in the correct sequence. The problem is that when you put them together you get "10/10/2000".
The system will treat this as "dd/MM/yyyy" or "MM/dd/yyyy" depending on the user's regional settings, and that is where the problem comes in.
Here is the solution:
Use the DateSerial function, because it allows you to tell the system which part is which.
Code:
'Get the parts into a DateTime variable
MyDate = DateSerial(txtFPODYear.Text , txtFPODMonth.Text , txtFPODDay.Text )
'Do your stuff
TempBatch = SunFiscal(Format(MyDate, "Whatever format YOU want"))
Hope this helps.
Shrog