Snippets

Coel May International Fixed Calendar Date

Created by Coel May
/*
 * PUBLIC DOMAIN.
 *
 * Interntational Fixed Calendar Date
 * 
 * Single function to return the current day of year based on the
 * International Fixed Calendar
 * https://en.wikipedia.org/wiki/International_Fixed_Calendar
 * This was larger Object/Proto based but have moved it into a singular.
 *
 * Date Object checking courteous of "Borgar"
 * http://stackoverflow.com/questions/1353684/ \
 * detecting-an-invalid-date-date-instance-in-javascript
 *
 * Day of year via "kennebec"
 * http://stackoverflow.com/questions/8619879/ \
 * javascript-calculate-the-day-of-the-year-1-366
 *
 * Leap Year via "Alex Turpin"
 * http://stackoverflow.com/questions/8619879/ \
 * javascript-calculate-the-day-of-the-year-1-366
 *
 * `ret` is the type of return value.
 *		 "Array", "Object" and "String" (default) are options.
 *		 If 'ret' is not one of these, an object is returned.
 * `dateObj` is a date object that can be provided to work on any
 * 			 date. Default is to work on current date.
 *			 If anything other than a date object is supplied, a
 *			 new date object is created using the current date.
 */
(function () {
	"use strict";
	/*
	 * dateObj, and it it really a `object Date`? */
	var now = new Date(),
		short = true,
		/*
		 * Our array of months. */
		months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Sol",
				  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
		/*
		 * Day of the year. */
		doy = (function (now) {
			var j1 = new Date(now);
			j1.setMonth(0, 0);
			return Math.round((now - j1) / 8.64e7);
		})(now),
		/*
		 * If this a leap year? */
		ly = (now.getFullYear() % 4 == 0 ? true : false),
		/*
		 * What is our month number.
		 * This one gave me the most grief. Is there a better way? */
		mno = (ly && ((doy % 28) === 0)) ?
				((doy > 169 && ((doy % 28) === 0)) ?
					Math.floor((doy - 1) / 28 )
					: Math.floor((doy + 1) / 28))
				: Math.floor(doy / 28),
		month = (short) ? months[mno].substr(0,3):months[mno],
		/*
		 * Day of the Month. */
		dom = (ly && doy > 169) ? ((doy - 1) % 28) : (doy % 28),
		/*
		 * Day of the Week. */
		dow = (function (dom) {
			var days = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" ],
				dow = (dom % 7);
			return days[dow];
		})(dom),
		display = document.getElementById('today'),
		ret = undefined;
	/*
	 * If the last day of the year, it is "Year Day".
	 * Return this. */
	if ((ly && doy == 366) || !ly && doy == 365) {
		ret = String('Year Day ' + now.getFullYear());
	}
	/*
	 * If it is a leap year, and the day after June 28, it is "Sol Day".
	 * Return this. */
	if (ly && doy == 169 && ret === undefined) {
		ret = String('Sol Day ' + now.getFullYear());
	}
	/*
	 * Fixing some calculations:
	 * 1. Because some numbers have a zero remainder when divided by 28.
	 *    Slight issue.
	 * 2. At end of year, we end up with a 13th month. Yes, there are 13 months
	 *    in this calendar, but the array index starts at zero, so last month is
	 *    actually 12. */
	if (dom == 0) { dom = 28; mno--; }
	if (mno == 13) { mno = mno - 1; }
	/*
	 * What return format is wanted?
	 * Array, Object (default) or String.
	 * Return appropriate. */
	if ( ret === undefined) { ret = String(dow + " " + month + ' ' + dom + " " + now.getFullYear()); }
	if (display !== null && ret !== undefined) {
		display.innerHTML = ret + ' (IFC)<br>' + (new Date().toDateString()) + ' (ABT)';
	}
})(window.document);

Comments (0)