﻿
////////////////////////////////////////////////////////////////////////////////////////////////////
// Date
////////////////////////////////////////////////////////////////////////////////////////////////////

Date.prototype.addDays = function(iDays)
{
    var date = new Date(this);
    date.setDate(date.getDate() + iDays);
    return date;
}

Date.prototype.addHours = function(iHours)
{ return new Date(this.getTime() + iHours * 1000 * 60 * 60); }

Date.prototype.addMilliseconds = function(iMilliseconds)
{ return new Date(this.getTime() + iMilliseconds); }

Date.prototype.addMinutes = function(iMinutes)
{ return new Date(this.getTime() + iMinutes * 1000 * 60); }

Date.prototype.addMonths = function(iMonths)
{
    var d = new Date(this);
    d.setMonth(this.getMonth() + iMonths);
    return d;
}

Date.prototype.addSeconds = function(iSeconds)
{ return new Date(this.getTime() + iSeconds * 1000); }

Date.prototype.addYears = function(iYears)
{
    var d = new Date(this);
    d.setFullYear(this.getFullYear() + iYears);
    return d;
}

Date.prototype.extractDate = function()
{
    var date = new Date(this);
    date.setHours(0, 0, 0, 0);
    return date;
}

Date.prototype.equals = function(iDate)
{ return !(iDate < this) && !(iDate > this); }

Date.prototype.format = function(iFormat, iCulture)
{
    if (!this.valueOf())
        return '&nbsp;';

    var cultureDate = (iCulture) ? iCulture.Date : Cultures.Current.Date;
    var d = this;
    var h = d.getHours() % 12;
    var formats = {
        'yyyy': d.getFullYear(),
        'mmmm': cultureDate.Months.Full[d.getMonth()],
        'mmm': cultureDate.Months.Short[d.getMonth()].substr(0, 3),
        'mm': ((d.getMonth() + 1)).toString().padLeft('0', 2),
        'm': ((d.getMonth() + 1)).toString(),
        'dddd': cultureDate.Weekdays.Full[d.getDay()],
        'ddd': cultureDate.Weekdays.Short[d.getDay()].substr(0, 3),
        'dd': d.getDate().toString().padLeft('0', 2),
        'd': d.getDate(),
        'HH': d.getHours().toString().padLeft('0', 2),
        'hh': (h ? h : 12).toString().padLeft('0', 2),
        'ii': d.getMinutes().toString().padLeft('0', 2),
        'ss': d.getSeconds().toString().padLeft('0', 2),
        'a/p': d.getHours() < 12 ? 'AM' : 'PM'
    };
    return iFormat.replace(/yyyy|mmmm|mmm|mm|dddd|ddd|dd|d|HH|hh|ii|ss|a\/p/gi,
        function(match) { return formats[match]; }
    );
}

////////////////////////////////////////////////////////////////////////////////////////////////////