DateUtility.js

var monthShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

function getDateTime() {
    var date = new Date();

    var hour = date.getHours();
    hour = (hour > 12 ? hour - 12 : hour);
    hour = (hour < 10 ? "0" : "") + hour;

    var min  = date.getMinutes();
    min = (min < 10 ? "0" : "") + min;

    var sec  = date.getSeconds();
    sec = (sec < 10 ? "0" : "") + sec;

    var year = date.getFullYear();

    var month = date.getMonth() + 1;
    month = (month < 10 ? "0" : "") + month;

    var day  = date.getDate();
    day = (day < 10 ? "0" : "") + day;

    return year + ":" + month + ":" + day + " - " + hour + ":" + min + ":" + sec;
}

/**
 * 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));
}

/**
 * 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();
	return date.getDate() + ' ' + monthShort[date.getMonth()] + ' ' + date.getFullYear();
}

/**
 * Get date after incrementing days
 * @param  {Date} date Date object/Timestamp
 * @param  {Number} days Increment/decrement date by given number
 * @return {Date}      Date object for next day
 */
function addDays(date, days) {
    date = new Date(date);
    date = date.setDate(date.getDate() + days);
    return new Date(date);
}

/**
 * Get date after incrementing months
 * @param  {Date} date Date object/Timestamp
 * @param  {Number} days Increment/decrement month by given number
 * @return {Date}      Date object for next month
 */
function addMonths(date, months) {
    date = new Date(date);
    date = date.setMonth(date.getMonth() + months);
    return new Date(date);
}

/**
 * Get date after incrementing months
 * @param  {Date} date Date object/Timestamp
 * @param  {Number} days Increment/decrement month by given number
 * @return {Date}      Date object for next month
 */
function addYears(date, years) {
    date = new Date(date);
    date = date.setFullYear(date.getFullYear() + years);
    return new Date(date);
}

/**
 * Convert timestamp from local timezone to GMT
 * @param  {Date} date Date/Timestamp
 * @return {Number}      Timestamp in GMT
 */
function getGmtTimestamp(timestamp, offset) {
    // Convert local time to GMT
    timestamp = timestamp + (offset * 60 * 1000);
    return timestamp;
}

/**
 * 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) {
        isWithinRange = date >= fromDate && date < toDate;
    }
    else if(isToInclusive) {
        isWithinRange = date > fromDate && date <= toDate;
    }
    else if(isFromInclusive && isToInclusive) {
        isWithinRange = date >= fromDate && date <= toDate;
    }
    else {
        isWithinRange = date > fromDate && date < toDate;
    }

    return isWithinRange;
}

module.exports = {
    addDays: addDays,
    addMonths: addMonths,
    addYears: addYears,
    getDateTime: getDateTime,
    getTimeStr: getTimeStr,
    getDateStr: getDateStr,
    getDateOnly: getDateOnly,
    getDateEnd: getDateEnd,
    getGmtTimestamp: getGmtTimestamp,
    compareDateOnly: compareDateOnly,
    isBetweenDates: isBetweenDates
};