A few User Defined Functions that I use all the time...
Enjoy.
<cfscript>
// This returns the Sunday DATE of the Week of the year passed in.
function YEARWEEK() {
var datein = NOW();
var yearin = YEAR(NOW());
var weekin = WEEK(NOW())-1;
IF(ArrayLen(Arguments) IS 1 AND IsDate(Arguments[1])) {
datein = Arguments[1];
}
yearin = YEAR(datein);
weekin = WEEK(datein)-1;
if (weekin IS 0) {
weekin = 52;
yearin = yearin-1; // first couple days in WK 1 count for end of previous YR
}
return toString(yearin) & toString(Numberformat(weekin,"00"));
}
// This returns the Sunday DATE of the Week-of-the-year passed in in YYYYWW format.
function YWtoDate(yw) {
var year = LEFT(yw,4);
var week = INT(RIGHT(yw,2));
if (LEN(yw) NEQ 6) {
return DateFormat(NOW(),'yyyy-mm-dd');
} else {
//return DateFormat(DateAdd('d',((week-1)*7)-(DayOfWeek('#year#-01-01')-1),'#year#-01-01'),'yyyy-mm-dd');
return DateFormat(DateAdd('ww',(week-1),DateAdd('d',#IIF(DayOfWeek('#year#-01-01') IS 1,0,8-DayOfWeek('#year#-01-01'))#,'#year#-01-01')),'yyyy-mm-dd');
}
}
// This returns the monday of the week date passed in.
function Monday() {
var monday = NOW();
var currentDay = DayOfWeek(monday);
IF(ArrayLen(Arguments) GTE 1 AND IsDate(Arguments[1])) {
monday = Arguments[1];
currentDay = DayOfWeek(monday);
}
if (currentDay IS 2) { //already monday
return DateFormat(monday, "yyyy-mm-dd");
} else if (currentday is 1) {
return DateFormat(DateAdd('d', 1, monday), "yyyy-mm-dd");
} else {
return DateFormat(DateAdd('d', -(currentDay-2), monday), "yyyy-mm-dd");
}
}
// To get the suffix on [1st, 2nd, etc.] use this:
function numbersuffix(thenumber) {
return IIF(VAL(RIGHT(thenumber,2)) GT 10 AND VAL(RIGHT(thenumber,2)) LT 20,
DE('th'),
DE(IIF(VAL(RIGHT(thenumber,1)) IS 1,
DE('st'),
DE(IIF(VAL(RIGHT(thenumber,1)) IS 2,
DE('nd'),
DE(IIF(VAL(RIGHT(thenumber,1)) IS 3,
DE('rd'),
DE('th')
) )
) )
) )
);
}
</cfscript>