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

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