Getting locale format for Windows - windows

how can I get correct locale's format for Windows in Delphi ?
I trying to do next
LCID := 2048;
FormatSettings := TFormatSettings.Create(LCID);
but this doesn't work fine if set shortdate format as example '07-13\2012'.
and variable will be equal
FormatSettings = 'MM/dd\yyyy' ?????

You could use this?
var
formatSettings : TFormatSettings;
begin
// Furnish the locale format settings record
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
// And use it in the thread safe form of CurrToStrF
ShowMessage('1234.56 formats as = '+
CurrToStrF(1234.56, ffCurrency, 4, formatSettings));
end;
http://www.delphibasics.co.uk/RTL.asp?Name=GetLocaleFormatSettings

in fact you should consider date as:
TShortDateFormatParts = (sdfpPrefix, sdfpDatePart1, sdfpSplitter1, sdfpDatePart2, sdfpSplitter2, sdfpDatePart3, sdfpSuffix);
in your code you should:
Find and get everything before leading "d", or "M" or "Y" (prefix).
Find and get text before first splitter.
Find and get end of first splitter.
Find and get text before second splitter.
Find and get end of second splitter.
Find and get everything before final text (suffix).
Get what we have now is final part
after:
Get position of DAY, MONTH and YEAR in current format string

The first lines of TFormatSettings.Create(Locale) are:
if not IsValidLocale(Locale, LCID_INSTALLED) then
Locale := GetThreadLocale;
When I pass LOCALE_SYSTEM_DEFAULT (2048) as my locale, IsValidLocale returns false and GetThreadLocale returns 4105 (Canadian-English). You may want to investigate this further. Are you getting the locale that you expecting?

The correct format of the locale for countries
var
formatSettings : TFormatSettings;
begin
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
ShowMessage('LOCALE_SYSTEM_DEFAULT = ' + DateTimeToStr(now, formatSettings));
GetLocaleFormatSettings(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), formatSettings);
ShowMessage('LANG_ENGLISH = ' + DateTimeToStr(now, formatSettings));
GetLocaleFormatSettings(MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT), formatSettings);
ShowMessage('LANG_RUSSIAN = ' + DateTimeToStr(now, formatSettings));
end;

Related

How to convert format of number from English to Italian with VBScript?

I need to convert the English numbering to Italian.
I tried using multiple replacements but I don't think it's the right method.
price = Replace(price, ".", ",")
The problem is that when I have too large numbers I get replaced several times and the wrong numbers come up.
For example:
English version: 3,450.108
After replacement: 3,450,108 (but it's wrong)
Correct format: 3.450,102
Would not recommend manually replacing values in an attempt to get the correct format when VBScript can already do it for you using the SetLocale() Function in conjunction with the FormatNumber() Function which will return the string representation of that number for the specific locale.
Note: Remember that the actual value and how a value is displayed are two separate things (see the example below).
Option Explicit
Const decimalplaces = 3
Dim price: price = 3450.108 'This is the raw value from your data source.
Call SetLocale("en-gb")
Call WScript.Echo("English (UK) price: " & FormatNumber(price, decimalplaces))
Call SetLocale("it-it")
Call WScript.Echo("Italian price: " & FormatNumber(price, decimalplaces))
Output:
English (UK) price: 3,450.108
Italian price: 3.450,108
If you want to swap 2 delimiter characters you need to use a temp character for the second one, otherwise you won't be able to distinguish between the original second delimiter and the replaced first one.
price = Replace(price, ".", "_")
price = Replace(price, ",", ".")
price = Replace(price, "_", ",")

Format Date() in VFP report

I use database program written in VFP and I am trying to change a date format in a report generated by that program.
The current field in the report is DATE() which returns DD/MM/YYYY
I'd like the field to return DDMMYY (ie no seperators, no century and single digit days and months to have a leading zero)
Can someone tell me the report field expression I should use to achieve this?
Thanks
I solved this problem by creating a procedure and call that procedure as
date2(date(), "", 2, 2, 2) in report field.
This will print 150418
This function can also be used with default value i.e. date(date()) which will print 15-Apr-18
FUNCTION date2(par_date, par_separtor, par_day_digit, par_month_digit, par_year_digit)
LOCAL date_return, day_t, month_t, year_t
IF VARTYPE(par_separtor) <> "C"
par_separtor = "-"
ENDIF
IF VARTYPE(par_year_digit) <> "N"
par_year_digit = 2
ENDIF
day_t = IIF(DAY(par_date)<10, '0', "") + allt(str(DAY(par_date)))
month_t = PROPER(allt(left(CMONTH(par_date),3)))
year_t = right(allt(str(year(par_date))),par_year_digit)
IF VARTYPE(par_month_digit) = "N"
month_t = IIF(MONTH(par_date)<10, '0', "") + allt(STR(MONTH(par_date)))
* MESSAGEBOX(month)
ENDIF
date_return = IIF(EMPTY(par_date), " ", day_t + par_separtor + month_t + par_separtor + year_t)
RETURN date_return
ENDFUNC
Before report set the century off if it is not already (you could do this even if you only have access to report itself, either in report environment or bands code):
Set century off
Then the expression is simply:
CHRTRAN(DTOC(DATE()),'/','')
Or if you meant a date field:
CHRTRAN(DTOC(myDateField),'/','')
If, for any reason, you can't use "set century off" (and it is ON) then change the expression slightly:
Stuff(CHRTRAN(DTOC(myDateField),'/',''),5,2,'')
Maybe this one is better and works regardless of date/datetime settings:
PADL(DAY(DATE())*10000+MONTH(DATE())*100+YEAR(DATE())%100,6,'0')
(if it is a date fieldthen replace date() with the field name)

VB 6.0 - Formating Textbox "dd/MM/yy"

I've made a question previously about this but I didn't express really what I wanted. I have a textbox that requires the user to write a Date in it. Then he can press a button and generate a report based on the entries on some other textboxes. It is really important that the date in the generated report is in this format "dd/MM/yy".
I use this code :
Dim a as String
a = Format(Textbox1.text, "dd/MM/yy")
Everything is working fine except when the user types a date in this format "dd.MM.yy" e.g 15.05.15 . When this happens, the date in the generated report is always "30/12/99". Can some1 help me understand why this is happening ?
EDIT: Regarding the Duplicate Question. In the previous question I asked how I can extract characters from a textbox and form a date in the format I want. This question is about a specific problem that happens when a specific type of format is used in the textbox (dd.MM.yy) that prints a specific date always (30/12/99).
30/12/1899 is the Date equivalent of 0, this is what you get when you try to convert a String which is not in a correct date format to a Date.
To verify this, type any old rubbish into your text box and you will get 30/12/99 as output.
Regarding using a DateTimePicker control, see my answer to your other question here
you can chose which keys you allow in the textbox.
for example as follows:
'1 form with:
' 1 textbox : name=Text1
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
' Caption = CStr(KeyAscii)
KeyAscii = DateOnly(KeyAscii)
End Sub
Private Function DateOnly(intKey As Integer) As Integer
Dim intResult As Integer
intResult = intKey
Select Case intKey
Case vbKeyBack 'allow backspace
Case vbKey0 To vbKey9 'allow numbers
Case 45 'allow -
' Case 46 'change . into /
' intResult = 47
Case 47 'allow /
Case Else 'dont allow anything else
intResult = 0
End Select
DateOnly = intResult
End Function
this just limits which keys the user can enter, you will still have to pay attention to other invalid inputs
[EDIT]
I added Case 45 to the code above.
In the select case you specify what happens to each key input:
you allow the key unaltered by specifying nothing, as i did with the / (ascii value 47)
you can change the key to another key, as i did with the . (ascii value 46)
you can reject the key input by setting it to 0, as i did with all other keys (case else)
You can find out which ascii value a specific key has by uncommenting the first line in Text1_KeyPress so the ascii value will show in the form caption
The IsDate Function is also useful. This will check if the date entered is valid, and return True if it is.
IsDate ("21/05/2015") 'returns 'True'
IsDate ("21.05.2015") 'returns 'False'
You could use the Replace function to change "." to "/"
mydate = Replace("21.05.2015", ".", "/") ' gives "21/05/2015"

Select in ADO (vb6) with a numeric variable

Excuse me, occasionally I refer with some problem that maybe it's already been fixed. In any case, I would appreciate a clarification on vs.
I have a TariffeEstere table with the fields country, Min, Max, tariff
from which to extract the rate for the country concerned, depending on whether the value is between a minimum and a maximum and I should return a single record from which to extract its tariff:
The query is:
stsql = "Select * from QPagEstContanti Where country = ' Spain '
and min <= ImpAss and max >= ImpAss"
Where ImpAss is a variable of type double.
When I do
rstariffa.open ststql,.....
the recodset contains a record if e.g. ImpAss = 160 (i.e. an integer without decimals), and then the query works, but if it contains 21,77 ImpAss (Italian format) does not work anymore and gives me a syntax error.
To verify the contents of the query string (stsql) in fact I find:
Select * from QPagEstContanti Where country = 'Spain' and min < = 21,77 and max > = 21,77
in practice the bothering and would like a comma decimal, but do not know how do.
I tried to pass even a
format (ImpAss, "####0.00"),
but the value you found in a stsql is 21,77 always.
How can I fix the problem??
It sounds like the underlying language setting in SQL is expecting '.' decimals instead of ',' decimal notation.
To check this out - run the DBCC useroptions command and see what the 'language' value is set to. If the language is set to English or another '.' decimal notation - it explains why your SQL string is failing with values of double.
If that's the problem, the simplest way to fix it is to insert the following line after your stsql = statement:
  stsql = REPLACE(stsql, ",", ".")
Another way to fix it would be to change the DEFAULT_LANGUAGE for the login using the ALTER LOGIN command (but this changes the setting permanently)
Another way to fix it would be to add this command to the beginning of your stsql, which should change the language for the duration of the rs.Open:
  "SET LANGUAGE Italian;"

How to Find Record Set Value is Number or String?

Original:
Using VB6
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Getting Type MisMatch Error.
How To Find Record Set Value is String or Number
Exactly i need
If Number means print number like Time Format (HH:MM:SS)
else
print string value
Coding Help for the above condition
Edited Version:
I'm working with an ADO.Recordset object and am trying to determine the data type of a column at run-time. I need to handle the column value differently in my code depending on its underlying data type. If the column value is a string, I want to work with the value as-is. If it is a number, I want to treat the number as an packed time and convert it to HH:MM:SS format (i.e. the number 120537 would be converted to the string "12:05:37").
Below is some example code that demonstrates what I want to achieve. However, when I run this code I get a "Type Mismatch" error:
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Have a look at the Visual Basic 6 function library. There are functions that can help you determine the underlying type of a value.
There are quite a few but you might find these useful:
IsNumeric
IsDate
Based on this article, if rsCardEvent is an ADO recordset, you could check the Type property. Something like this:
Select Case rsCardEvent(4).Type
Case adBSTR, adChar, adVarChar, adWChar, _
adVarWChar, adLongVarChar, adLongVarWChar
' It is a string '
Case Else
' It is not a string '
End Select
You can use the IsNumeric function available in VB6.
How about:
If TypeName(rsCardEvent(4).Value) = "String" then
http://msdn.microsoft.com/en-us/library/5422sfdf.aspx

Resources