0

var date1 = 13/10/2016 04:35;

var date2 = 27/03/2017 08:00;

var diff = date2 - date1

How do i subtract the two javascript dates that are in the above format and know the number of days between them and return the result in DD/Month/Year format. Also i will like to add 180days to date1 and also output the result in DD/Month/Year format? Thanks for your help.

ossy
  • 1
  • 1
  • 2
  • 2
    A quick google would have found you the answer: https://momentjs.com/ – Callum Linington Mar 27 '17 at 08:12
  • 3
    Possible duplicate of [How to add/subtract dates with javascript?](http://stackoverflow.com/questions/10931288/how-to-add-subtract-dates-with-javascript) – Callum Linington Mar 27 '17 at 08:13
  • 2
    A minified version of `moment` is quite a large file (48kb) so I would definitely advise against using it if this is your only use case for it. – Dan Mar 27 '17 at 08:17
  • thanks a lot very helpful – ossy Mar 27 '17 at 12:52
  • You're asking 3 questions ([*parsing*](http://stackoverflow.com/questions/10638529/how-to-parse-a-date-in-format-yyyymmdd-in-javascript), [*formatting*](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|20.7697), [*adding days*](http://stackoverflow.com/questions/9989382/add-1-to-current-date)) all of which have many questions and answers, without showing an attempt at any of them. – RobG Mar 27 '17 at 13:15

1 Answers1

0

If you take a look at the Date Object and its methods and properties in JavaScript, you'll notice that JavaScript bases its time on milliseconds since 1970.

Knowing that you can easily modify dates by adding or subtracting milliseconds.

/**
 * subtractDateFromDate
 * Use the millisecond value insert both elements to calculate a new date.
 * @param d1 {Date}
 * @param d2 {Date}
 */
function subtractDateFromDate(d1, d2) {
    return new Date(d1.getTime() - d2.getTime());
}
/**
 * addDaysToDate
 * Create a new Date based on the milliseconds of the first date,
 * adding the milliseconds for the extra days specified.
 * Notice that days can be a negative number.
 * @param d {Date}
 * @param days {number}
 */
function addDaysToDate(d, days) {
    var oneDayInMilliseconds = 86400000;
    return new Date(d.getTime() + (oneDayInMilliseconds * days));
}

And to format the date without moment.js:

/**
 * formatDate
 * returns a string with date parameters replaced with the date objects corresponding values
 * @param d {Date}
 * @param format {string}
 */
function formatDate(d, format) {
    if (format === void 0) { format = "!d!/!m!/!y!"; }
    return format
        .replace(/!y!/ig, d.getFullYear().toString())
        .replace(/!m!/ig, (d.getMonth() + 1).toString())
        .replace(/!d!/ig, d.getDate().toString());
}
//Testing
var d = new Date();
console.log("d/m/y:", formatDate(d, "!d!/!m!/!y!"));
console.log("m/d/y:", formatDate(d, "!m!/!d!/!y!"));
console.log("It was the d of m, y:", formatDate(d, "It was the !d! of !m!, !y!"));
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • Tidy little formatter, but you might have chosen an existing token scheme rather than invent a new one—there are already plenty to choose from! ;-) BTW, much better to add to the date rather than add milliseconds to the time since not all days are 8.64e7ms long where daylight saving is observed. – RobG Mar 27 '17 at 13:10