在安排活动时显示美国假期的日历

时间:2012-12-13 18:22:05

标签: sql-server-2008 sql-server-2005 coldfusion

我的客户通常会提前几个月为她的客户安排约会。如果预订发生在美国假日日/周期间,我的客户希望通知美国假期期间的日程安排日期(她在假期收取更多费用)。

我的客户只关注以下日期:

  • 新年12月30日至1月2日
  • 三月春假周(三月第三周)
  • 复活节周末(此日期每年不同)
  • 五月阵亡将士纪念日
  • 七月独立日周末
  • 九月劳动节周末
  • 感恩节周一至周日11月
  • 圣诞节12月22日至28日

我想尽可能地自动化这个,所以她不必每年更新数据库,但这不是她的要求,而是我的要求。 :D最好是将假日日期/规则输入数据库,还是使用规则来确定约会的日期是否在预约的日期/星期或假日日期/周内?

如果数据库是答案,我需要一些数据库设计方面的帮助,以及如何设置事件的重现。如果基于规则,我也需要一些帮助(例如确定春假,复活节等的时间)。

我正在使用MSSQL 2005(可能很快就会转向mssql 2008)和ColdFusion 9。

TIA

4 个答案:

答案 0 :(得分:3)

上个月我wrote a UDF帮助许多美国假期,但并未涵盖所有假期。这应该会给你一个不错的开始。

你也可以使用这个能让你复活节的UDF from CFLib

一旦你有阵亡将士纪念日,你可以做一个简单的dateadd()并带走1天和2天来度过整个周末。

<cffunction name="getUSBankHolidays" access="public" output="false" returntype="struct" hint="general bank holidays for US">
  <cfargument name="iYear" default="#Year(now())#" />

    <cfset var currentYear = arguments.iYear />

    <!--- Static dates per year --->
    <cfset var strResult = 
      { NewYears1      = createDate(currentYear,12,30),
        NewYears2      = createDate(currentYear,12,31),
        NewYears3      = createDate(currentYear,1,1),
        NewYears4      = createDate(currentYear,1,2),
        Independence   = createDate(currentYear,7,4),
        Christmas1     = createDate(currentYear,12,22),
        Christmas2     = createDate(currentYear,12,23),
        Christmas3     = createDate(currentYear,12,24),
        Christmas4     = createDate(currentYear,12,25),
        Christmas5     = createDate(currentYear,12,26),
        Christmas6     = createDate(currentYear,12,27),
        Christmas7     = createDate(currentYear,12,28)
  } />

    <cfset strResult.SpringBreak          = createDate(currentYear,3,GetNthOccOfDayInMonth(3,1,3,currentYear)) />
    <cfset strResult.MemorialDay          = createDate(currentYear,5,(DaysInMonth(createDate(2012,5,1))) - (DayOfWeek(createDate(2012,5,DaysInMonth(createDate(2012,5,1)))) - 2)) />
    <cfset strResult.LaborDay             = createDate(currentYear,9,GetNthOccOfDayInMonth(1,2,9,currentYear)) />
    <cfset strResult.Thanksgiving         = createDate(currentYear,11,GetNthOccOfDayInMonth(4,6,11,currentYear)) />

    <cfreturn strResult />
</cffunction>

<cfdump var="#getUSBankHolidays()#">

<!-- This code will tell you if the date is holiday or not --->
<cfset Today = '2012-01-01' />
<cfdump var="#NOT arrayIsEmpty(structFindValue(getUSBankHolidays(),createDate(year(Today),month(Today),day(Today))))#">

您还需要来自CFLib.org的this UDF才能获得一个月中第n天的出现。

答案 1 :(得分:1)

数据库表将为您提供更大的灵活性。

我帮助维护的数据仓库有一个名为period的表,其中包含主键的日期。其他领域包括假期,一些财政年度的东西,以及其他一些对我们很重要的领域。其他表具有对表的外键引用。

除了使我们能够轻松生成必要的报告之外,此表还允许我们使用左连接来标识未发生某些事情的日期。这样的事情在你的情况下可能会有用。

就更新而言,我们是手动完成的。当圣诞节这样的事情发生在周末时,我们会等待周五或周一的关闭。

答案 2 :(得分:0)

在我看来,我会把它放在代码中。几年前我发现了这个问题并根据许可协议使用了它。它是基于Java的,但我相信如果你需要,你可以改为Coldfusion。它目前列出了66个假期的代码,并确保你可以操纵它来计算你需要的一周。

http://mindprod.com/applet/holidays.html

http://mindprod.com/precis/holidays.txt

https://wush.net/websvn/mindprod/listing.php?repname=mindprod&path=%2Fcom%2Fmindprod%2Fholidays%2F

答案 3 :(得分:0)

这是我最终使用的,并且足够灵活,可以在将来进行修改,我仍然可以将其转换为数据库驱动的系统,其中包含用于添加未来日期的GUI,但是现在,这非常有效。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.8.3.js"></script> 
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css" /> 
<script> 
$(document).ready(function(){ 
  $( ".datepicker" ).datepicker({ 
    changeMonth: true, 
    changeYear: true 
  }); 
}); 
</script> 
</head> 

<body> 
<cfoutput> 
<form action="#cgi.SCRIPT_NAME#" method="post"> 
  Start Date: <input type="text" name="StartDate" class="datepicker" /> (mm-dd-yyyy)<br /> 
  End Date: <input type="text" name="EndDate" class="datepicker" /> (mm-dd-yyyy) <br /> 
  <input type="submit" value="Save Appointment" /> 
</form> 
<cfif structkeyexists(form,'startDate')> 
<cfdump var="#form#" expand="no"> 
<hr /> 
<cfscript> 
/** 
* Returns the day of the month(1-31) of an Nth Occurrence of a day (1-sunday,2-monday etc.)in a given month. * 
* @param NthOccurrence      A number representing the nth occurrence.1-5. 
* @param TheDayOfWeek      A number representing the day of the week (1=Sunday, 2=Monday, etc.). 
* @param TheMonth      A number representing the Month (1=January, 2=February, etc.). 
* @param TheYear      The year. 
* @return Returns a numeric value. 
* @author Ken McCafferty (mccjdk@yahoo.com) 
* @version 1, August 28, 2001 
*/
function GetNthOccOfDayInMonth(NthOccurrence,TheDayOfWeek,TheMonth,TheYear) 
{
  Var TheDayInMonth=0; 
  if(TheDayOfWeek lt DayOfWeek(CreateDate(TheYear,TheMonth,1))){ 
    TheDayInMonth= 1 + NthOccurrence*7  + (TheDayOfWeek - DayOfWeek(CreateDate(TheYear,TheMonth,1))) MOD 7; 
  } 
  else { 
    TheDayInMonth= 1 + (NthOccurrence-1)*7  + (TheDayOfWeek - DayOfWeek(CreateDate(TheYear,TheMonth,1))) MOD 7; 
  } 
  //If the result is greater than days in month or less than 1, return -1
  if(TheDayInMonth gt DaysInMonth(CreateDate(TheYear,TheMonth,1)) OR   TheDayInMonth lt 1){ 
    return -1; 
  } 
  else { 
    return TheDayInMonth; 
  } 
} 
/** 
* Returns the date for Easter in a given year.
* Minor edits by Rob Brooks-Bilson (rbils@amkor.com).
* 
* @param TheYear      The year to get Easter for. 
* @return Returns a date object. 
* @author Ken McCafferty (rbils@amkor.commccjdk@yahoo.com) 
* @version 1, September 4, 2001 
*/
function GetEaster() {
  Var TheYear=iif(arraylen(arguments) gt 0,"arguments[1]", "Year(Now())"); 
  Var century = Int(TheYear/100);
  Var G = TheYear MOD 19;
  Var K = Int((century - 17)/25);
  Var I = (century - Int(century/4) - Int((century - K)/3) + 19*G + 15) MOD 30;
  Var H = I - Int((I/28))*(1 - Int((I/28))*Int((29/(I + 1)))*Int(((21 - G)/11)));
  Var J = (TheYear + Int(TheYear/4) + H + 2 - century + Int(century/4)) MOD 7;
  Var L = H - J;
  Var EasterMonth = 3 + Int((L + 40)/44);
  Var EasterDay = L + 28 - 31*Int((EasterMonth/4));
  return CreateDate(TheYear,EasterMonth,EasterDay);
}

function LastDayOfMonth(strMonth) {
  var strYear=Year(Now());
  if (ArrayLen(Arguments) gt 1)
    strYear=Arguments[2];
  return DateAdd("d", -1, DateAdd("m", 1, CreateDate(strYear, strMonth, 1))); 
}
</cfscript>

<cfset StartYear = listlast(StartDate,'/')>
<cfset EndYear = listlast(endDate,'/')>
<cfset HolidaySchedule = structnew()>
<cfloop index="currentYear" from="#dateformat(StartDate,'yyyy')#" to="#dateformat(EndDate,'yyyy')#" step="1"> 
<cfset val = StructInsert( HolidaySchedule, "Independence (#currentYear#)", createDate(currentYear,7,4) )/> 
<cfset val = StructInsert( HolidaySchedule, "New Year (#currentYear#)", createdate(currentYear,1,1) )/> 
<cfset val = StructInsert( HolidaySchedule, "Easter (#currentYear#)", GetEaster(currentYear) )/> 
<cfset val = StructInsert( HolidaySchedule, "Spring Break (#currentYear#)", createDate(currentYear,3,GetNthOccOfDayInMonth(2,2,3,currentYear)) )/>
<cfset val = StructInsert( HolidaySchedule, "Labor Day (#currentYear#)", createDate(currentYear,9,GetNthOccOfDayInMonth(1,2,9,currentYear)) )/>
<cfset val = StructInsert( HolidaySchedule, "Thanksgiving (#currentYear#)", createDate(currentYear,11,GetNthOccOfDayInMonth(4,6,11,currentYear)) )/>
<cfset val = StructInsert( HolidaySchedule, "Christmas Day (#currentYear#)", createdate(currentYear,12,25) )/>
</cfloop>
<cfdump var="#HolidaySchedule#" expand="no">
<cfset normalweek = 'true'>
<cfloop index="x" from="#startDate#" to="#endDate#" step="1">
Checking date: #dateformat(x,'yyyy-mm-dd')#<br />
<cfif NOT arrayIsEmpty(structFindValue(HolidaySchedule,createDate(year(x),month(x),day(x))))> 
<div style="background-color:##009900; color:##ffffff">#dateformat(x, 'mm/dd/yyyy')# is a holiday.</div> 
<cfset normalweek = 'false'>
</cfif>
</cfloop>

<cfif normalweek eq 'false'>
Remember to add on the Holiday Fee.
<cfelse>
No additional charges for this appointment.
</cfif>
</cfif>
</cfoutput>
</body>
</html>