1

Given a start and an end date, I would like to know the percentage that has been achieved from the start date to the current date relatively to the end date. In other words: with a given date, I would want to know in percent how far I am still to the end date. But if I try that as shown in the code, I get a utopian number (-97%).

const isToday = (durchlauf) => {

  var start = new Date(2020, 12, 1); 
  var end = new Date(2021, 1, 1); 
  const today = new Date();
  var total = end - start;
  var progress = today - start;

  console.log(Math.round((progress / total) * 100) + "%");

 //OUTPUT
 -97%

};

Returns a value of -97%, why?

Shimi
  • 1,178
  • 8
  • 18
schorle88
  • 103
  • 6
  • percentage of what - 1 year? Or how much further into the year? Please provide more example input/outputs – WOUNDEDStevenJones Dec 01 '20 at 18:29
  • Why start date is in 2021? Shouldn't today must be a date after the start date? – Ozgur Sar Dec 01 '20 at 18:30
  • @WOUNDEDStevenJones Well between the start and end date with reference to the current date ?! – schorle88 Dec 01 '20 at 18:31
  • Because this is an application where the user can choose the start date himself ... – schorle88 Dec 01 '20 at 18:31
  • 1
    Again, please provide more example input/outputs so it's clear to us and future readers what logic you are trying to implement. As the question is currently written, it's not apparent at all - there are a dozen different ways to interpret which % you're looking for. – WOUNDEDStevenJones Dec 01 '20 at 18:34
  • Try `console.log(start)`. Months are zero-indexed. If you want December 1st 2020 you need `new Date(2020, 11, 1)` – Guy Incognito Dec 01 '20 at 18:41
  • You are forgetting one important point JavaScript counts months from 0 to 11. January is 0. December is 11. – Sachin Yadav Dec 01 '20 at 18:44

3 Answers3

1

The good news is that your code is correct. The only problem you are seeing is the differences and inconsistencies with how the Date constructor is being set (the month argument). Once I set the start & end days as a string rather than sending arguments, your logic worked as expected, and the result was a positive 2%. You can also send arguments, but be mindful to initialize the value of the month correctly as suggested in the comments.

var start = new Date("12/1/2020"); // new Date(2020, 11, 1);
var end = new Date("1/1/2021"); // new Date(2021, 0, 1);
const today = new Date();
var total = end - start;
var progress = today - start;

console.log(Math.round((progress / total) * 100) + "%");
Shimi
  • 1,178
  • 8
  • 18
1

If you look at the Docs

new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])

Or from the ECMAScript Docs:

If month is present, let m be ? ToNumber(month); else let m be 0.

so the Month should be represented (for hystorical reasons) as an index. Index start at 0, not at 1. December 1st: therefore is new Date(2020, 11, 1) using 11, not 12

Some more info here

Progress percentage between two dates example:

const getProgress = (startDate, endDate) => {
  const total = +endDate - +startDate;
  const elaps = Date.now() - start;
  return Math.round((elaps / total) * 100) + "%";
};

const start = new Date(2018, 0, 1);   // START: Jan 1, 2018 
const end = new Date(2025, 11, 31);   // END: Dec 31, 2025
console.log(getProgress(start, end)); // PROGRESS: 36% (at Dec 1, 2020)
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Thanks for the response buddy, just an FYI, the first link of 'Docs' is leading to a 404. – Shimi Dec 01 '20 at 20:04
0

You just set the start date as 2021/1/2. But today is before that start date so you can get correct progress value.

devastro
  • 79
  • 3
  • Ok I understand. But why do I get this value (-97%) when entering: start = new Date (2020,12,1) end = new Date (2012,1,1)? – schorle88 Dec 01 '20 at 18:37
  • And then you just get incorrect value because the end date is before the start date. So you can't get the correct total value. – devastro Dec 01 '20 at 18:40