Over a million developers have joined DZone.

Get Annual Event Name

·

Object.extend(Date.prototype, {
  getAnnualEventName:
    function() {
      var annualEvents = $H({
            'newyear':        '1/1-1/3',
            'haloween':       '10/31',
            'christmas':      '12/1-12/25',
      });

      var today = new Date(this.getYear(), this.getMonth(), this.getDate());
      var annualEventName = '';

      annualEvents.each(
                        function(pair, index) {
                          days = pair[1].split('-');
                          if(!days[1]) {
                            if(today.getTime() ==
                               new Date(this.getYear(),
                                        days[0].split('/')[0] - 1, days[0].split('/')[1]).getTime()) {
                                annualEventName = pair[0];
                            }
                          } else {
                            from = new Date(this.getYear(),
                                            days[0].split('/')[0] - 1, days[0].split('/')[1]);
                            to = new Date(this.getYear(),
                                          days[1].split('/')[0] - 1, days[1].split('/')[1]);
                            if(from > to) {
                              if(!(today > to && today < from)) {
                                annualEventName = pair[0];
                              }
                            } else {
                              if(today >= from && today <= to) {
                                annualEventName = pair[0];
                              }
                            }
                          }
                        }.bind(this)
                        );

      return annualEventName;
    }
});
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}