0

I want to write a JavaScript function, which will compare 2 date values (startdate & now) and then show:-

  1. the remaining months & weeks.

  2. if the start date in with in the current month, to show the remaining weeks & days.

  3. if the start date is with in the current week, to show the renaming days.

now I find this script :

var nurl = items[i].CounterStartDate.toString();
var countDownDate = new Date(nurl).getTime();


var now = new Date().getTime();

// Find the distance between now an the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

which will show the remaining days, hours, minutes and seconds. But I am not sure how I can modify it to match my above 3 points?

Hint. i date values i am using comes from a rest api and it will have the following format 2019-05-24T23:00:00Z or 2018-06-20T23:00:00Z, etc..

John John
  • 1
  • 72
  • 238
  • 501
  • 4
    Simplest is use a library like [moment.js](http://momentjs.com/) which has duration methods – charlietfl Apr 29 '18 at 00:40
  • @charlietfl i am trying to find a solution wihout external libraries if possible – John John Apr 29 '18 at 00:59
  • @johnG bite the rather small bullet and use moment.js. It'll make your life so much simpler and its really quite small. There really is no valid argument against it, just "But I don't wanna" – Wesley Smith Apr 29 '18 at 02:49
  • 1
    In coding we often have to choose **when to reinvent the wheel and when not to**! I think that moment.js is the perfect answer to all datetime related issues in javascript. Its very stable and almost all top level software product companies use it successfully. @johnG – vibs2006 Apr 29 '18 at 03:05
  • Possible duplicate of [Difference between two dates in years, months, days in JavaScript](https://stackoverflow.com/questions/17732897/difference-between-two-dates-in-years-months-days-in-javascript) – Heretic Monkey Apr 29 '18 at 03:06
  • or [Display real time years, months, weeks and days between 2 days in JavaScript](//stackoverflow.com/q/2935554) – Heretic Monkey Apr 29 '18 at 03:06

1 Answers1

2

I think this function will do what you want.

function datediff(date) {
   let d1 = date;
   let d2 = now = new Date();
   if (d2.getTime() < d1.getTime()) {
     d1 = now;
     d2 = date;
   }
   let yd = d1.getYear();
   let yn = d2.getYear();
   let years = yn - yd;
   let md = d1.getMonth();
   let mn = d2.getMonth();
   let months = mn - md;
   if (months < 0) {
     years--;
     months = 12 - md + mn;
   }
   let dd = d1.getDate();
   let dn = d2.getDate();
   let days = dn - dd;
   if (days < 0) {
     months--;
     // figure out how many days there are in the last month
     d2.setMonth(mn, 0);
     days = d2.getDate() - dd + dn;
   }
   let weeks = Math.floor(days / 7);
   days = days % 7;
   if (years > 0) return years + ' years' + (months > 0 ? ' and ' + months + ' months' : '');
   if (months > 0) return months + ' months' + (weeks > 0 ? ' and ' + weeks + ' weeks' : '');
   if (weeks > 0) return weeks + ' weeks' + (days > 0 ? ' and ' + days + ' days' : '');
   return days + ' days';
}

Output:

console.log(datediff(new Date('2018-04-24')))
5 days
console.log(datediff(new Date('2018-04-04')))
3 weeks and 4 days
console.log(datediff(new Date('2018-03-30')))
4 weeks and 2 days
console.log(datediff(new Date('2018-03-28')))
1 months
console.log(datediff(new Date('2018-03-20')))
1 months and 1 weeks
console.log(datediff(new Date('2017-12-03')))
4 months and 3 weeks
console.log(datediff(new Date('2017-02-03')))
1 years and 2 months
console.log(datediff(new Date('2018-05-12')))
1 weeks and 5 days
Nick
  • 138,499
  • 22
  • 57
  • 95
  • now i try to pass this value `datediff(new Date("2022-05-12"))` but i got the following result `Starts in 11 months and 2 weeks`, can u please advice on this?.. i also pass this `datediff(new Date("2018-05-12"))` but i got the same result as ` Starts in 11 months and 2 weeks`!! – John John Apr 29 '18 at 22:27
  • I had mentioned at the start of my answer that the function would only work if the date input was in the past as I had assumed from your question that you only needed that functionality. In my edit I have modified the function so that it will also work with dates in the future. – Nick Apr 29 '18 at 23:52
  • thanks for the update , i did a quick test and seems they are working well.. but in ur code the date format inside the users's Pcs must be dd-mm-yyyy ,,, is this correct? otherwise the code will mix between day and month – John John Apr 30 '18 at 11:29
  • 1
    Hi John, the interpretation of the date is up to Javascript. I've just used ISO format dates (yyyy-mm-dd) because I'm used to typing them. But you could use a date in any format Javascript recognises; as to whether it is treated dd-mm-yyyy or mm-dd-yyyy will be dependent on the locale that the computer is in. For example, if I do datediff(new Date('5/1/18')) I get 1 day because I'm in Australia (and it's April 30 here). However someone in the US would get 3 months and 3 weeks because the date would be interpreted as January 5. – Nick Apr 30 '18 at 12:55
  • 1
    Check out the manual on [Date.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) for all the possible arguments to new Date() and how they are interpreted in terms of days and months. – Nick Apr 30 '18 at 12:57