Recent Weblogs

Links I like

Next

GregorianCalendar an extension of ObjectRange

In my pursuit to develop a date chooser widget I came across the idea to extend the ObjectRange class from prototype. This would ease my troubles in creating a range of dates representing a span of time.

ObjectRange prototype's Ranged Enumeration class

ObjectRange "mixes" in with Enumerable, which means it inherits a bunch of great functions to enumerate over an internal data set. In most cases it is a range of numbers or characters, but with a little help it can easily handle a range of Date objects.

Extension Neccesary

It needed a succ method, this is called within the ObjectRange's to increment to the next value, I figured the next day would be perfect for this. Becareful though the inequality operator is precise down to the millisecond you've got to copy your object's wisely for it to iterate all the way through.

To define just an arbitrary range of dates was so easy with ObjectRange it didn't even require a class extension. You just need to implement the succ method into the date object's prototype. When I needed more logic to define the end of the range, i decided to extend the class with some easy to work with constants to retrieve max days and the corresponding English name of the month, such as January, February etc.

  • Download the source
  • Read More about where the grid class goes, now that we have calendar data and a grid injection class, its time to set up the DTO.
  • Learn more about ObjectRange the super class of the GregorianCalendar class
GregorianCalendar
Constructor
GregorianCalendarinitialize(Date obj)
Uses a date object to retrieve year and month information.
GregorianCalendarinitialize(integer year, integer month)
Explicitly set year and month with integers. Months correlate as 0-11.
Methods
DatebuildDate(Date)
Returns a date object of the same year and month but set to the very beginning of the month, down to the millisecond which is required for proper enumeration.
DatebuildDate(integer year, integer month)
Explicitly set year and month with integers. Months correlate as 0-11.
CONSTANTS
MONTH_MAP["January", "February", "March", "April", "May", "June", "July", "August", "Septmeber", "October", "November", "December"]
A convienence array to help with interpreting the getMonth() method to an English month name.
DAY_MAX_MAP[31,00,31,30,31,30,31,31,30,31,30,31]
An array constant to help with determining the max day of the corresponding month, February is 0 as to detect easily in a conditional and apply a method.

Comments are Disabled.