Date Words
Bob Sneidar
bobsneidar at iotecdigital.com
Wed Jun 12 11:20:47 EDT 2024
Hi all.
Did you ever want to use phrases like yesterday, last tuesday or next friday in a date field?
function dateWords pDate
if word 1 of pDate is not among the items of "last,next,yesterday,today,tomorrow" then \
return empty
put date() into tCurrentDate
convert tCurrentDate to dateItems
if the number of words of pDate = 2 then
put "sunday,monday,tuesday,wednesday,thursday,friday,saturday" into tDaysOfWeek
put itemOffset(word 2 of pDate, tDaysOfWeek) into tDayNumber
put item 7 of tCurrentDate into tThisDayNumber
if tDayNumber = 0 then \
return empty
end if
switch
case word 1 of pDate is "last"
if tDayNumber >= tThisDayNumber then \
subtract 7 from item 3 of tCurrentDate
add (tDayNumber - tThisDayNumber) to item 3 of tCurrentDate
break
case word 1 of pDate is "next"
add 7-tDayNumber to item 3 of tCurrentDate
break
case pDate is "yesterday"
subtract 1 from item 3 of tCurrentDate
break
case pDate is "today"
-- don't do anything
break
case pDate is "tomorrow"
add 1 to item 3 of tCurrentDate
break
end switch
put formatDate(tCurrentDate, "standard") into pDate
return pDate
end dateWords
FUNCTION formatDate theDate, theFormat
/*
Accepts any valid date for the first parameter. If not a valid date, it simply returns
what was passed. Second parameter can be any of the following:
sql date: date in the yyyy-mm-dd format
short date, abbreviated date, internet date, long date: LC versions of the same
julian date: Julian number based on (I believe) Jacques formula
standard date: The date in the form of "mm/dd/yyyy"
*/
put theDate into tSavedDate
put the itemdelimiter into theOldDelim
set the itemdelimiter to "-"
IF the length of item 1 of theDate = 4 AND \
the number of items of theDate = 3 AND \
item 1 of theDate is a number AND \
item 2 of theDate is a number AND \
item 3 of theDate is a number THEN
put item 2 of theDate & "/" & \
item 3 of theDate & "/" & \
item 1 of theDate into theDate
END IF
-- replace "." with "/" in theDate
convert theDate to dateitems
set the itemdelimiter to theOldDelim
-- if the number of items of theDate <> 7 then
-- answer "'" & theDate & "' is not a valid date format!"
-- return tSavedDate
-- end if
SWITCH word 1 of theFormat
CASE "sql"
/*
put item 1 of theDate & "-" & \
format("%02d",item 2 of theDate) & "-" & \
format("%02d",item 3 of theDate) into theDate
*/
put format("%s-%02d-%02d", item 1 of theDate, item 2 of theDate, \
item 3 of theDate) into theDate
break
CASE "short"
convert theDate from dateitems to short date
break
CASE "abbreviated"
convert theDate from dateitems to abbreviated date
break
CASE "abbr"
convert theDate from dateitems to abbreviated date
break
CASE "internet"
convert theDate from dateitems to internet date
break
CASE "long"
convert theDate from dateitems to long date
break
CASE "julian"
put the date into theDate
convert theDate to dateItems
IF ((item 2 of theDate = 1) OR (item 2 of theDate = 2)) THEN
put 1 into theDay
ELSE
put 0 into theDay
END IF
put item 1 of theDate + 4800 - theDay into theYear
put item 2 of theDate + (12 * theDay) - 3 into theMonth
put item 3 of theDate + \
((153 * theMonth + 2) div 5) + \
(365 * theYear) + \
(theYear div 4) - \
(theYear div 100) + \
(theYear div 400) - \
32045 into theDate
break
case "standard"
put format("%02d/%02d/%04d", item 2 of theDate, item 3 of theDate, \
item 1 of theDate) into theDate
break
default
-- answer info "'" & theFormat & "' is not a valid parameter." As sheet
put tSavedDate into theDate
END SWITCH
return theDate
END formatDate
Bob S
More information about the use-livecode
mailing list