MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Calendar Recipe?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112649] Re: Calendar Recipe?
  • From: dr DanW <dmaxwarren at gmail.com>
  • Date: Fri, 24 Sep 2010 04:11:20 -0400 (EDT)
  • References: <i7f2mv$ctt$1@smc.vnet.net>

This isn't quite what you are looking for, but the techniques you need
are here.  I think you can extend the MonthMatrix function into a
QuarterMatrix function with a few simple changes.  This is code I
wrote to create a Dynamic control for choosing dates out of a
calendar, similar to a JavaScript or ActiveX control you would see on
a travel web site:

WeekDayNumber[d_] := DateString[d, "DayNameShort"] /.
   {"Sun" -> 1, "Mon" -> 2, "Tue" -> 3, "Wed" -> 4, "Thu" -> 5, "Fri" -
> 6, "Sat" -> 7}

QuarterStartMonth[d_] := DateString[d, "Quarter"] /. {"1" -> 1, "2" ->
4, "3" -> 7,
    "4" -> 10}

DateSameQ[d1_, d2_] := d1[[1 ;; 3]] === d2[[1 ;; 3]]

FirstDayOf[d_, "Week"] := DatePlus[d, {1 - WeekDayNumber[d], "Day"}];
FirstDayOf[d_, "Month"] := DateList[d[[1 ;; 2]]];
FirstDayOf[d_, "Quarter"] := DateList[{d[[1]],
QuarterStartMonth[d]}];
FirstDayOf[d_, "Year"] := DateList[d[[{1}]]];

MonthMatrix[d_] := Partition[(DatePlus[FirstDayOf[FirstDayOf[d,
"Month"], "Week"],
      {#1, "Day"}] & ) /@ (Range[1, 6*7] - 1), 7]

Attributes[CalendarControl] = HoldFirst;

CalendarControl[date_, init_:DateList[]] := (date =
DateList[init[[1 ;; 3]]];
   Dynamic[Panel[Grid[Join[{{Button[Style["\[FirstPage]", Large],
          If[DateSameQ[date, FirstDayOf[date, "Quarter"]],
           date = DatePlus[date, {-3, "Month"}], date =
FirstDayOf[date, "Quarter"]],
          Appearance -> "None"], Button[Style["\[LeftPointer]",
Large],
          date = DatePlus[date, {-1, "Month"}], Appearance ->
"None"],
         Dynamic[DateString[date, {"MonthNameShort", " ", "Year"}]], \
[SpanFromLeft], \[SpanFromLeft],
         Button[Style["\[RightPointer]", Large], date = DatePlus[date,
{1, "Month"}],
          Appearance -> "None"], Button[Style["\[LastPage]", Large],
          If[DateSameQ[date, FirstDayOf[date, "Quarter"]],
           date = DatePlus[date, {3, "Month"}], date =
DatePlus[FirstDayOf[date,
              "Quarter"], {3, "Month"}]], Appearance -> "None"]}},
       {{"S", "M", "T", "W", "T", "F", "S"}},
       Map[Button[If[#1[[2]] === date[[2]], Style[#1[[3]]],
Style[#1[[3]],
            Opacity[0.3]]], date = #1, Appearance -> "None",
          If[#1[[1 ;; 3]] === date[[1 ;; 3]], Background ->
ColorData["Legacy"][
             "PaleGreen"], Background -> Automatic], BaseStyle ->
           {FontSize -> 10}] & , MonthMatrix[date], {2}]],
      Dividers -> {None, {3 -> True}}]]])

CalendarControl[sd]

Dynamic[DateString[sd]]

Enjoy.

Daniel


  • Prev by Date: Re: Animation
  • Next by Date: Re: How to deal with this message?
  • Previous by thread: Re: Calendar Recipe?
  • Next by thread: Animation