Round Date by year, Month, day, hour, minute, second - Node.js Date

Node.js examples for Date:Year

Description

Round Date by year, Month, day, hour, minute, second

Demo Code

String.prototype.pad = function (char, length) {
    return this.length >= length ? this : pad(this, char, 2);
};

Date.prototype.clean = function () {
    return formatDate(this, 'yyyy-mm-dd hh:nn:ss').clean();
};

Date.prototype.getWeek = function () {
    var onejan = new Date(this.getFullYear(), 0, 1);
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
};

Date.prototype.roundYear = function () {
    return new Date(this.getFullYear(), 0, 0, 0, 0, 0, 0);
};

Date.prototype.roundMonth = function () {
    return new Date(this.getFullYear(), this.getMonth(), 0, 0, 0, 0, 0);
};

Date.prototype.roundDay = function () {
    return new Date(this.getYear(), this.getMonth(), this.getDate(), 0, 0, 0, 0);
};

Date.prototype.roundHour = function () {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), 0, 0, 0);
};

Date.prototype.roundMinute = function () {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), 0, 0);
};

Date.prototype.roundSecond = function () {
    return new Date(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), 0);
};


String.prototype.clean = function () {
    var result = this;
    result = result.replace(/ /g, '');
    result = result.replace(/\./g, '');
    result = result.replace(/-/g, '');
    result = result.replace(/\\/g, '');
    result = result.replace(/\//g, '');
    result = result.replace(/:/g, '');
    result = result.replace(/{/g, '');
    result = result.replace(/}/g, '');
    result = result.replace(/"/g, '');
    return result;
};

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, '');
};

String.prototype.ltrim = function () {
    return this.replace(/^\s+/, '');
};

String.prototype.rtrim = function () {
    return this.replace(/\s+$/, '');
};

String.prototype.fulltrim = function () {
    return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ');
};


Date.prototype.dateDiff = function (comparedtodate) {

    var first = this;
    // Copy date parts of the timestamps, discarding the time parts.
    var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());
    var two = new Date(comparedtodate.getFullYear(), comparedtodate.getMonth(), comparedtodate.getDate());

    // Do the math.
    var millisecondsPerDay = 1000 * 60 * 60 * 24;
    var millisBetween = two.getTime() - one.getTime();
    var days = millisBetween / millisecondsPerDay;

    // Round down.
    return Math.floor(Math.abs(days));
}

Related Tutorials