![Laughing :lol:](images/smilies/icon_lol.gif)
I also created something to do a kind of emulation by
f_formatDate(date, formatstring), e.g.
? f_formatDate(dtDt,"dddd dd-MMM-yyyy hh:mm ap")
there are also functions:
f_DayOfWeek(date )
f_DayName(date)
f_MonthName(date)
- Code: Select all
function f_DayOfWeek(dtDate as date, iFirstDayOfWeek as integer = 0 ) as integer
/*
Function calculating day of the week for dtDate
Day Number 1 by default is Sunday but this can be modified
by setting iFirstDayOfWeek.
iFirstDayOfWeek = 1 moves day number 1 to Monday and so on.
So without setting iFirstDayOfWeek the function returns
1 for Sunday, 2 for Monday..
with iFirstDayOfWeek set to 1 the function returns
1 for Monday, 2 for Tuesday..
*/
Dim iDayDiff as integer
Dim iDay as integer
Dim dBase as date
Dim iOut as integer
'31.12.1899 was sunday
dBase = #1899-12-31#
dBase = dateadd("d",iFirstDayOfWeek,dBase)
iDayDiff = datediff("d",dtDate,dBase)
iOut = (iDayDiff mod 7) + 1
if iOut <= 0 then iout = 7 + iout
return iOut
end function
function f_DayName(dtDate as date) as string
/*
Function calculating DayName for dtDate
*/
Dim iDayDiff as integer
Dim iDay as integer
Dim dBase as date
Dim strOut as string
'31.12.1899 was sunday
dBase = #1899-12-31#
iDayDiff = datediff("d",dtDate,dBase)
iDay = (iDayDiff mod 7) + 1
if iDay <= 0 then iDay = 7 + iDay
select case iDay
case 1: strOut = "Sunday"
case 2: strOut = "Monday"
case 3: strOut = "Tuesday"
case 4: strOut = "Wednesday"
case 5: strOut = "Thursday"
case 6: strOut = "Friday"
case 7: strOut = "Saturday"
case else: strOut = ""
end select
return strOut
end function
function f_MonthName(dtDate as date) as string
/*
Function calculating MonthName for dtDate
*/
Dim iMonth as integer = month(dtDate)
Dim strOut as string
select case iMonth
case 1: strOut = "January"
case 2: strOut = "February"
case 3: strOut = "March"
case 4: strOut = "April"
case 5: strOut = "Mai"
case 6: strOut = "June"
case 7: strOut = "July"
case 8: strOut = "August"
case 9: strOut = "September"
case 10: strOut = "October"
case 11: strOut = "November"
case 12: strOut = "December"
case else: strOut = ""
end select
return strOut
end function
function f_formatDate( dtDate as date, strFormat as string) as string
/* emulates format(date,format)
convention : Midnight is 12:00 AM while noon is 12:00 PM
Uses:
f_DayName( dtDate as Date )
f_MonthName( dtDate as Date )
*/
Dim iarrA[1] as integer
Dim strF as string
Dim strL as string
Dim strC as string
Dim iCnt as integer
Dim j as integer
Dim iNum as integer
Dim iBound as integer
Dim strOut as string
Dim strFmt as string
Dim intTime as integer
Dim intH as integer
Dim intM as integer
Dim intS as integer
Dim iMax as integer
Dim strStamp as string
'set AM or PM in case of time related format
intTime = hour(dtDate)*3600 + Minute(dtDate)*60 + Second(dtDate)
strFmt = strFormat
strStamp = "a"
iCnt = instr(1,UCase(strFmt),"AP")
if iCnt > 0 then
strC = "a"
intTime = intTime mod 86400
if intTime >= 43200 then
strStamp = "pm"
if mid(strFmt,iCnt+1,1)="P" then strC = UCase(strC)
else
strStamp = "am"
if mid(strFmt,iCnt,1)="A" then strC = UCase(strC)
end if
strFmt = remove(strFmt , iCnt , 2)
strFmt = insert(strFmt , strC , iCnt)
intTime = (intTime - 3600) Mod (43200)
If intTime < 0 Then intTime = intTime + 43200
intTime = intTime + 3600#
end if
intH = intTime \ 3600
intM = (intTime - intH * 3600) \ 60
intS = intTime - IntH * 3600 - intM * 60
'Analize format string
strF = ""
strL = ""
strOut = ""
iBound = 0
iarrA[iBound]=0
for iCnt = 1 to len(strFmt)
strC = Mid(strFmt,iCnt,1)
select case strC
case "y","M","d" : iMax = 4
case "h","m","s" : iMax = 2
case "z": iMax = 1
case "a","A": iMax = 1
case else : iMax = 100
end select
if (strC <> strL) or iarrA[iBound]>=iMax then
strF = strF & strC
iBound = len(strF)
redim preserve iarrA(iBound)
iarrA[iBound]=1
else
iarrA[iBound]=iarrA[iBound]+1
end if
strL = strC
next
'create Output string
for iCnt = 1 to len(strF)
strC = Mid(strF,iCnt,1)
Select case strC
case "d":
iNum = day(dtDate)
j = iarrA[iCnt]
select case j
case 1: strOut = strOut & inum
case 2: strOut = strOut & right("0" & inum,2)
case 3: strOut = strOut & left(f_DayName(dtDate),3)
case else:
strOut = strOut & f_DayName(dtDate)
end select
case "M":
iNum = Month(dtDate)
j = iarrA[iCnt]
select case j
case 1: strOut = strOut & inum
case 2: strOut = strOut & right("0" & inum,2)
case 3: strOut = strOut & left(f_MonthName(dtDate) ,3)
case else:
strOut = strOut & f_MonthName(dtDate)
end select
case "y":
iNum = year(dtDate)
j = iarrA[iCnt]
select case j
case 1: strOut = strOut & "y"
case 2: strOut = strOut & right("0" & inum,2)
case 3: strOut = strOut & right("0" & inum,2) & "y"
case else:
strOut = strOut & right("000" & inum,4)
end select
case "h":
iNum = intH
j = iarrA[iCnt]
select case j
case 1: strOut = strOut & inum
case else:
strOut = strOut & right("0" & inum,2)
end select
case "m":
iNum = intM
j = iarrA[iCnt]
select case j
case 1: strOut = strOut & inum
case else:
strOut = strOut & right("0" & inum,2)
end select
case "s":
iNum = intS
j = iarrA[iCnt]
select case j
case 1: strOut = strOut & inum
case else:
strOut = strOut & right("0" & inum,2)
end select
case "z":
strOut = strOut & "0"
case "A":
strOut = strOut & UCase(strStamp)
case "a":
strOut = strOut & strStamp
case else:
for j = 1 to iarrA[iCnt]
strOut = strOut & strC
next
end select
next
return strOut
end function
sub Main()
Dim strDt as String
Dim dtDt As datetime
strDt = "1977-09-05"
dtDt = Date(strDt)
? "1: " & f_formatDate(CDate(strDt),"yyyy-MM-dd")
? "2: " & f_formatDate(datevalue(strDt),"yyyy-MMMM-dd")
? "3: " & f_formatDate(dtDt,"dddd dd-MMM-yy")
? "4: " & f_formatDate(dateserial(year(dtDt), month(dtDt), day(dtDT) ),"yyyy-MM-dd")
? "5: " & f_formatDate(now(),"yyyy-MM-dd hh:mm:ss ap")
? "6: " & f_formatDate(#2001-01-31#,"yyyy-MMM-dd")
end sub
Main()