DateUtility.js

angular.module('homepage').
factory('DateUtility', function() {
    var monthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

    return {
        getMonthTitles: getMonthTitles,
        getTimeStr: getTimeStr,
        getDateStr: getDateStr,
        getDateOnly: getDateOnly,
        getDateEnd: getDateEnd,
        getMonthStart: getMonthStart,
        getMonthEnd: getMonthEnd,
        getMonthDateRange: getMonthDateRange,
        getYearStart: getYearStart,
        getYearEnd: getYearEnd,
        getGmtTimestamp: getGmtTimestamp,
        newDateSameRef: newDateSameRef,
        compareDateOnly: compareDateOnly,
        isBetweenDates: isBetweenDates
    }

    function getMonthTitles(isShort) {
        var monthLong = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        var monthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        
        if(isShort) {
            return monthShort;
        } else {
            return monthLong;
        }
    }

    /**
     * Get time from date as string
     * @param  {Date} date Date object/Timestamp
     * @return {string}      Time as string 'hh:mm:ss am/pm'
     */
    function getTimeStr(date) {
			date = new Date(date) || new Date();
			var hours = date.getHours();
			var minutes = date.getMinutes();
			var seconds = date.getSeconds();
			var ampm = hours >= 12 ? 'pm' : 'am';
			hours = hours % 12;
			hours = hours ? hours : 12; // the hour '0' should be '12'
			minutes = minutes < 10 ? '0' + minutes : minutes;
			seconds = seconds < 10 ? '0' + seconds : seconds;
			var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
			return strTime;
		}

    /**
     * Get date from date as string
     * @param  {Date} date Date object/Timestamp
     * @return {string}      Date as string 'dd MMM yyyy'
     */
	function getDateStr(date) {
        date = new Date(date) || new Date();
        var monthShort = getMonthTitles(true);
		return date.getDate() + ' ' + monthShort[date.getMonth()] + ' ' + date.getFullYear();
	}

    /**
     * Get date only with time part set to zero
     * @param  {Date} date Date object/Timestamp
     * @return {Date}      Date object
     */
    function getDateOnly(date) {
    	date = new Date(date);
    	return new Date(date.setHours(0,0,0,0));
    }

    /**
     * Get date only with time part set to zero
     * @param  {Date} date Date object/Timestamp
     * @return {Date}      Date object
     */
    function getDateEnd(date) {
        date = new Date(date);
        return new Date(date.setHours(23,59,59,999));
    }

    function getMonthStart(date) {
        return new Date(date.getFullYear(), date.getMonth(), 1);
    }

    function getMonthEnd(date) {
        return new Date(date.getFullYear(), date.getMonth() + 1, 0);
    }

    function getMonthDateRange(date) {
        date = new Date(date);
        var firstDay = getMonthStart(date);
        var lastDay = getMonthEnd(date);
        return {
            start: getDateOnly(firstDay),
            end: getDateEnd(lastDay)
        };
    }

    function getYearStart(date) {
        return new Date(date.getFullYear(), 0, 1);
    }

    function getYearEnd(date) {
        return new Date(date.getFullYear(), 11, 31);
    }

    /**
     * Convert timestamp from local timezone to GMT
     * @param  {Date} date Date/Timestamp
     * @return {Number}      Timestamp in GMT
     */
    function getGmtTimestamp(date) {
        if(!date) {
            date = new Date();
        }
        // Timestamp in client time zone
        var timestamp = new Date(date).getTime();
        var offset = new Date().getTimezoneOffset(); // in minutes
        // Convert local time to GMT
        // TODO add or substract based on the offset value
        timestamp = timestamp + (offset * 60 * 1000);
        return timestamp;
    }

    /**
     * Get latest date keeping the refrence of the passed date object
     * @param  {Date} date Date object/Timestamp
     * @return {Date}      Date object
     */
    function newDateSameRef(date) {
        date = new Date(date);
        date.setTime(new Date().getTime());
        return date;
    }

    /**
     * Compare dates without comparing time
     * @param  {Date} date1 Date object/Timestamp
     * @param  {Date} date2 Date object/Timestamp
     * @return {Number}     Difference in dates after setting time to zero
     */
    function compareDateOnly(date1, date2) {
    	date1 = getDateOnly(date1);
    	date2 = getDateOnly(date2);

    	var oneday = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    	var diff   = (date1.getTime() - date1.getTime())/ oneday;

    	return diff;
    }

    /**
     * Check if the given date falls between from and to date
     * @param  {Date}  date            Date to be compared
     * @param  {Date}  fromDate        From Date
     * @param  {Date}  toDate          To Date
     * @param  {Boolean} isFromInclusive Is from date inclusive
     * @param  {Boolean} isToInclusive   Is to date inclusive
     * @return {Boolean}                 [description]
     */
    function isBetweenDates(date, fromDate, toDate, isFromInclusive, isToInclusive) {
        var isWithinRange = false;

        date        = new Date(date);
        fromDate    = getDateOnly(fromDate);
        toDate      = getDateEnd(toDate);

        if(isFromInclusive && isToInclusive) {
            isWithinRange = date >= fromDate && date <= toDate;
        }
        else if(isFromInclusive) {
            isWithinRange = date >= fromDate && date < toDate;
        }
        else if(isToInclusive) {
            isWithinRange = date > fromDate && date <= toDate;
        }
        else {
            isWithinRange = date > fromDate && date < toDate;
        }

        return isWithinRange;
    }

});