0

I've specified a specific time on a certain day. Now I wish to calculate the milliseconds from the beginning of that specific day to the set time of that day? I was hoping to do that with below code, but instead it shows nothing? What am I doing wrong?

var now = new Date().getTime();
var oneday = 1000 * 60 * 60 * 24;
var countDownDate = new Date("January 10, 2018 00:01").getTime();
var countDownStart = new Date(countDownDate.getFullYear(), countDownDate.getMonth(), countDownDate.getDate(), 0, 0, 0, 0); 
var countDownTime = countDownDate.getTime() - countDownStart.getTime();

var div = document.getElementById('result'); 
div.innerText = countDownTime;

I specify the countDownDate. Then I mark the beginning of that countDownDate into the variable countDownStart. Next I calculate the time passed since 00:00 of January 10 to 00:01 of January 10 by subtracting countDownStart from countDownDate. But no result is shown...

jiggy1965
  • 157
  • 1
  • 2
  • 14
  • 2
    You can use moment library.it will be usefull – zabusa Jan 06 '18 at 13:56
  • @31piy sometimes its easy to understand moment.doing these much of stuff. – zabusa Jan 06 '18 at 14:00
  • @31piy nowadays there are lots of bundlers and methods to reduce the bundle size if he needs to load the library he lazyload also.so that's depends – zabusa Jan 06 '18 at 14:05
  • @31piy ohk..but also there is lots of methods to tackle that.eg.zip.lazy loading etc.. So readability is to be taken care. – zabusa Jan 06 '18 at 14:10
  • Please don't use the built-in parser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). Instead of `new Date("January 10, 2018 00:01")` use `new Date(2018, 0, 10, 0, 1)` which is not only more robust but also less to type. ;-) Also, not all days are 24 hrs long where daylight saving is observed (some are longer, some shorter). – RobG Jan 06 '18 at 23:41

2 Answers2

2

Your code has only one issue, and that is that you've assigned the result of .getTime() to countDownDate, which will be a number.

That's why JavaScript cannot call getFullYear, or any other function on that number, because those will be invalid calls.

To correct that, just remove the .getTime(), and it will work fine.

var now = new Date().getTime();
var oneday = 1000 * 60 * 60 * 24;
var countDownDate = new Date("January 10, 2018 00:01");
var countDownStart = new Date(countDownDate.getFullYear(), countDownDate.getMonth(), countDownDate.getDate(), 0, 0, 0, 0);
var countDownTime = countDownDate.getTime() - countDownStart.getTime();

var div = document.getElementById('result');
div.innerText = countDownTime;
<div id="result">
  <div>
31piy
  • 23,323
  • 6
  • 47
  • 67
0

Your logic is fine here. The only issue is this line here:

var countDownDate = new Date("January 10, 2018 00:01").getTime();

Since you used .getTime() the variable countDownDate is no longer a date. As such in the following statementcountDownDate.getFullYear() and forward isn't going to work. Simply remove .getTime() and it will work as expected:

var now = new Date().getTime();
var oneday = 1000 * 60 * 60 * 24;
var countDownDate = new Date("January 10, 2018 00:01");
var countDownStart = new Date(countDownDate.getFullYear(), countDownDate.getMonth(), countDownDate.getDate(), 0, 0, 0, 0); 
var countDownTime = countDownDate.getTime() - countDownStart.getTime();

console.log(countDownTime)
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
  • There's no need for *getTime* at all, the difference can be just `countDownDate - countDownStart`. ;-) – RobG Jan 06 '18 at 23:45