Author |
Topic: FORMAT$ (Read 510 times) |
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: FORMAT$
« Reply #3 on: Nov 7th, 2015, 11:41am » |
|
This is what I've been working on. Untested as of now. Code:
function DateCode$(year,month,day,code$)
'function to format a date into YYYY-MMM-DD-X (e.g. X=“1”, “2”, “F”, “Y”, etc.)
year = int(abs(year)) : month = int(abs(month)) : day = int(abs(day)) : code$ = left$(code$,1) ' normalize values
code = asc(code$) : if (code < 48 and code > 57) and (code < 65 and code > 90) then code$ = "0" ' normalize value
select case ' work on the year entered
case year < 100 ' 2 digits "2000 - 2099"
year$ = "20" + right$("00" + str$(year),2) ' YYYY
case year < 2000 ' "1900 - 1999"
year$ = str$(year)
case year < 2100 ' "2000 - 2099"
year$ = str$(year)
case else
year$ = date$("yyyy")
end select
select case ' work on the month entered
case month > 0 and month < 10 ' 1 digit "1 - 9"
month$ = right$("00" + str$(month),2) ' MM
case month < 13 ' "10 - 12"
month$ = str$(month)
case else
month$ = date$("mm")
end select
select case ' work on the day entered
case day > 0 and day < 10 ' 1 digit "1 - 9"
day$ = right$("00" + str$(day),2) ' DD
case day < 32 ' "10 - 31"
day$ = str$(day)
case else
day$ = date$("dd")
end select
DateCode$ = year$ + "-" + month$ + "-" + day$ + "-" + code$
end function
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: FORMAT$
« Reply #4 on: Nov 7th, 2015, 12:00pm » |
|
on Nov 7th, 2015, 11:41am, pnlawrence wrote:| This is what I've been working on. |
|
For comparison, an API date-formatting function:
Code: print DateFormat$(2015, 11, 7, "yyyy-MM-dd")
print DateFormat$(2015, 11, 7, "yyyy-MMM-dd")
print DateFormat$(2015, 11, 7, "yyyy-MMMM-dd")
print DateFormat$(2015, 11, 7, "dddd yyyy MMMM dd")
end
function DateFormat$(year,month,day,format$)
struct st, Year as short, Month as short, DayOfWeek as short, Day as short, _
Hour as short, Minute as short, Second as short, Milliseconds as short
st.Year.struct = year
st.Month.struct = month
st.Day.struct = day
date$ = space$(100) + chr$(0)
cch = len(date$)
calldll #kernel32, "GetDateFormatA", 0 as long, 0 as long, st as struct, _
format$ as ptr, date$ as ptr, cch as long, r as long
DateFormat$ = trim$(date$)
end function Richard.
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: FORMAT$
« Reply #5 on: Nov 7th, 2015, 1:03pm » |
|
Fantastic!
All I have to do is add a bit to the code$, and I have the custom string that I want.
Good show, Richard! Thanks!
EDIT: Added silly error checking code. Who makes errors in data entry? :D
Code:
customNum = 99
code$ = "yyyy-MM-dd"
print DateCode$(2015, 11, 7, "dd-MM-yyyy")
print DateCode$(2015, 11, 7, "dd-MMM-yyyy")
print DateCode$(2015, 11, 7, "dd-MMMM-yyyy")
print DateCode$(2015, 11, 7, "dddd dd MMMM yyyy")
' test for bad numbers or simplified way to use today's date (put in nonsense data)
result$ = DateCode$(2014, 12, 12, code$ +"-"+str$(customNum))
if result$ = "" then
result$ = date$(code$) + "-" + str$(customNum) ' use today's date
end if
print result$
end
function DateCode$(year,month,day,code$)
struct st, Year as short, Month as short, DayOfWeek as short, Day as short, _
Hour as short, Minute as short, Second as short, Milliseconds as short
st.Year.struct = year
st.Month.struct = month
st.Day.struct = day
date$ = space$(100) + chr$(0)
cch = len(date$)
calldll #kernel32, "GetDateFormatA", 0 as long, 0 as long, st as struct, _
code$ as ptr, date$ as ptr, cch as long, r as long
DateCode$ = trim$(date$)
end function
|
| « Last Edit: Nov 7th, 2015, 1:33pm by joker » |
Logged
|
|
|
|
|