Javascript date add and diff - Node.js Date

Node.js examples for Date:Date Calculation

Description

Javascript date add and diff

Demo Code

// from http://trentrichardson.com/2011/09/27/better-javascript-date-add-and-diff/
Date.prototype.adjust = function (part, amount) {
    part = part.toLowerCase();// w  w  w  .  ja v  a 2s  . c o m
    var map = {
        years:'FullYear',
        months:'Month',
        weeks:'Hours',
        days:'Hours',
        hours:'Hours',
        minutes:'Minutes',
        seconds:'Seconds',
        milliseconds:'Milliseconds',
        utcyears:'UTCFullYear',
        utcmonths:'UTCMonth',
        utcweeks:'UTCHours',
        utcdays:'UTCHours',
        utchours:'UTCHours',
        utcminutes:'UTCMinutes',
        utcseconds:'UTCSeconds',
        utcmilliseconds:'UTCMilliseconds'
    };
    var mapPart = map[part];
    if (part == 'weeks' || part == 'utcweeks') amount *= 168;
    if (part == 'days' || part == 'utcdays') amount *= 24;
    this['set' + mapPart](this['get' + mapPart]() + amount);
    return this;
};

Related Tutorials