admin管理员组

文章数量:1304516

Working on a javascript-canvas based clock (classic analog clock view), also displaying the current date below the clock.

I already have this code to get the current time in javascript:

   // get current time
    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    hours = hours > 12 ? hours - 12 : hours;
    var hour = hours + minutes / 60;
    var minute = minutes + seconds / 60;

Works great, except that I don't know how to get the number in seconds until the end of the day, so I could run an ajax request at 00:00h to update the current date.

The question is, how to get easily the number in seconds until end of the day in javascript?

I plan to start a setTimeout()-function after the clock loaded with the number of seconds left, to update the date when needed.

Working on a javascript-canvas based clock (classic analog clock view), also displaying the current date below the clock.

I already have this code to get the current time in javascript:

   // get current time
    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    hours = hours > 12 ? hours - 12 : hours;
    var hour = hours + minutes / 60;
    var minute = minutes + seconds / 60;

Works great, except that I don't know how to get the number in seconds until the end of the day, so I could run an ajax request at 00:00h to update the current date.

The question is, how to get easily the number in seconds until end of the day in javascript?

I plan to start a setTimeout()-function after the clock loaded with the number of seconds left, to update the date when needed.

Share Improve this question asked Aug 1, 2014 at 20:43 lickmycodelickmycode 2,0793 gold badges19 silver badges20 bronze badges 2
  • How are you showing the clock? I'm guessing you update it every second right? Why not just do the same with the date. Update it every second and it will update to the new day automatically. – putvande Commented Aug 1, 2014 at 20:44
  • @putvande I don't update the clock, I just move the cursors in a radius by setInterval(1000). – lickmycode Commented Aug 1, 2014 at 21:19
Add a ment  | 

2 Answers 2

Reset to default 7

I'm assuming the date you want to change is not from these values. You need to change it in some place not directly related to this clock?

I would suggest to add a function to check if the day has changed and include it when the clock is refreshed.

In any case, getting the seconds to the end of the day should be something like

var secondsUntilEndOfDate = ( (24*60*60) - ( (hours*60*60) + (minutes*60) + seconds ) );

Javascript:

var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var secondsUntilEndOfDate = (24*60*60) - (h*60*60) - (m*60) - s;

For GMT+0 it would be

const secondUntilEndOfTheDay = 86400 - Math.floor(new Date() / 1000) % 86400;

本文标签: Javascript new Date()Get seconds until end of the dayStack Overflow