Format Date() in VFP report - visual-foxpro

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)

Related

Substracting one Hour from Date in ISO-8601 format using VBScript [duplicate]

I was wondering if someone could help me.
I'm very new at ASP I want to format the current date and time as follows:
yyyy-mm-dd hh:mm:ss
But all i can do is the following
Response.Write Date
Can someone help me out please.
Date formatting options are limited in Classic ASP by default, there is a function FormatDateTime() which can format your date is various ways based on the servers regional settings.
For more control over date formatting though there are built in date time functions
Year(date) - Returns a whole number representing the year. Passing Date() will give back the current year.
Month(date) - Returns a whole number between 1 and 12, inclusive, representing the month of the year. Passing Date() will return the current month of the year.
MonthName(month[, abbv]) - Returns a string indicating the specified month. Passing in Month(Date()) as the month will give back the current Month string. As suggested by #Martha
Day(date) - Returns a whole number between 1 and 31, inclusive, representing the day of the month. Passing Date() will return the current day of the month.
Hour(time) - Returns a whole number between 0 and 23, inclusive, representing the hour of the day. Passing Time() will return the current hour.
Minute(time) - Returns a whole number between 0 and 59, inclusive, representing the minute of the hour. Passing Time() will return the current minute.
Second(time) - Returns a whole number between 0 and 59, inclusive, representing the second of the minute. Passing Time() will return the current second.
IMPORTANT:
When formatting date / time values, always store the date / time value first. Also, any needed calculations (DateAdd() etc.) should be applied before attempting to format or you will get unexpected results.
The functions Month(), Day(), Hour(), Minute() and Second() all return whole numbers. Luckily there is an easy workaround that lets you pad these values quickly Right("00" & value, 2) what it does is append 00 to the front of the value then from the right take the first two characters. This ensures that all single digit values return prefixed with a 0.
Dim dd, mm, yy, hh, nn, ss
Dim datevalue, timevalue, dtsnow, dtsvalue
'Store DateTimeStamp once.
dtsnow = Now()
'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)
hh = Right("00" & Hour(dtsnow), 2)
nn = Right("00" & Minute(dtsnow), 2)
ss = Right("00" & Second(dtsnow), 2)
'Build the date string in the format yyyy-mm-dd
datevalue = yy & "-" & mm & "-" & dd
'Build the time string in the format hh:mm:ss
timevalue = hh & ":" & nn & ":" & ss
'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
dtsvalue = datevalue & " " & timevalue
Call Response.Write(dtsvalue)
Note: You can build the date string in one call but decided to break it down into the three variables to make it easier to read.
How Can I Format Date
Example of Parsing a Date String (Answers provide approaches to taking a date string format and parsing it to a valid Date variable).
Format the date of the previous day format yyyymmdd with VBScript (Example of why storing date / time before performing formatting is important)
VBScript ISO8601 (Example of functions to construct an ISO 8601 compliant date string)

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"

Format number with default NumDigAfterDec and without rounding

I am using in my sql server reports a simple vbscript command to format some numeric fields:
FormatNumber(value,,-1,0,-1)
Important for me is the second parameter NumDigAfterDec that is set to default, meaning
that "the computer's regional settings are used" (http://www.w3schools.com/vbscript/func_formatnumber.asp). This is exactly what I prefer. But when the current number has more digits after decimal, it is rounded. In this case I would like to see all places.
e.g. for two places after decimal, I would like:
0 0.00
90.7 90.70
1.2345 1.2345 (instead of 1.23)
Is this possible without writing code in my reports?
If I understand correctly, you want a condition stating if length of decimal is greater than 2 places, then display full decimal number, else 2 decimal places?
A simple way to do this is:
Dim value : value = "100.54367"
Dim GetDecCount : GetDecCount = Len(Mid(Value, instr(value, ".")+1, len(value)))
if GetDecCount>2 then
msgbox "This number exceeds the regional decimal points of 2, with a length of " & GetDecCount & " decimal points."
else
FormatNumber(value,,-1,0,-1)
end if
Try something like this:
Set re = New RegExp
re.Pattern = ".\d{3,}$"
if re.Test(value) Then
fnum = CStr(value)
Else
fnum = FormatNumber(value, , -1, 0, -1)
End If

Getting locale format for 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;

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