I am trying to get the current time difference in hours and minutes between now and 12pm (midday). If it is past 12pm on the current day it must then count down the hours onto the next day.
I have looked around here for a while now and have found many examples on getting date differences but and am unable to get a working solution for time so have been playing around with writing my own. However I am unable to getTime(); to get hours and minutes from the millisecond timestamp on my new dates and am unsure why exactly. Here is my code:
dateone = new Date();
datetwo = new Date();
datetwo = datetwo.setHours(12);
dateone = dateone.getTime();
datetwo = datetwo.getTime();
if(dateone > datetwo) {
var seconds = dateone - datetwo;
} else {
var seconds = datetwo - dateone;
}
var d = seconds;
var minutes=(d/(1000*60))%60;
var hours=(d/(1000*60*60))%24;
var minutesround=Math.floor(minutes);
var hoursround=Math.round(hours);
var endtime = 12;
alert(hours);
alert(minutes);
If anyone is able to spot a better way of doing this, or can suggest anything it would be greatly appreciated!
Thanks,
Simon
Try
var d = new Date(), midDay = new Date();
midDay.setHours(12);
midDay.setMinutes(0);
midDay.setSeconds(0);
if(d > midDay) {
midDay.setDate(midDay.getDate() + 1)
}
var diff = (midDay.getTime() - d.getTime()) / 1000;
var hrs = parseInt(diff / 3600);
diff = diff % 3600;
var minutes = parseInt(diff / 60);
console.log(hrs, minutes)
Demo: Fiddle
Related
I am trying to create a javascript countdown which displays the hours and minutes, counting down to midday each day. When midday is reached I would like the timer to reset and start counting down to midday again (obviously to countdown to the following day).
I have the code below, however I just can't get it to work properly, the code works fine after midday however once midnight is reached the count is incorrect.
Here is my code:
function ShowTimes() {
var now = new Date();
var hrtime = now.getHours()
var hrs = 23 - hrtime + 12;
var mins = 59-now.getMinutes();
var secs = 59-now.getSeconds();
var str = '';
str += hrs+' hours '+mins+' minutes';
document.getElementById('countdown').innerHTML = str;
}
var _cntDown;
function StopTimes() {
clearInterval(_cntDown);
}
Any help is much appreciated! Thanks in advance.
Do you live somewhere that moves clocks forwards/backwards in spring/autumn?
If so, you'll have two days in the year where your hour and minute logic would fail.
Here's a way that works even when the clocks change:
var now = new Date();
var midday = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (now.getHours() >= 12 ? 1 : 0), 12);
var millisToMidday = midday.getTime() - now.getTime();
var hours = Math.floor((millisToMidday / (60 * 60 * 1000)))
var minutes = Math.floor((millisToMidday / (60 * 1000))) % 60;
var seconds = Math.floor((millisToMidday / (1000))) % 60;
Change the logic for Hrs calculation as below :
if(hrtime>12)
hrtime=23- hrtime+ 12;
else
hrtime= 12-hrtime;
I have two Time Stamps in the following Format:
Start Time - 2016-01-01 00:00:00
Finish Time - 2016-01-02 23:15:00
I need to calculate the number of hours between the two date times correct to two decimal places.
I need the result to be in the following format
Hrs = 47.25
This would be 23 hours and .25 of an hour not 25 mins, I do not need to round to the nearest 15 I only need to round to 2 decimal places.
Any Help shall be much appreciated. This data will then be inserted back into a cell in a DHTMLX grid.
FINAL SOLUTION
adminGrid.attachEvent("onEditCell",function(stage,rId,cInd,nValue,oValue){
if ((cInd == colStartTime || cInd == colFinishTime) && stage == 2) {
var startTime = new Date(adminGrid.cells(rId,colStartTime).getValue());
var finishTime = new Date(adminGrid.cells(rId,colFinishTime).getValue());
var Hrs = ((finishTime - startTime)/1000/60/60).toFixed(2);
adminGrid.cells(rId,colHrs).setValue(Hrs);
}
return true;
});
var startDate = new Date('2016-01-01 00:00:00');
var endDate = new Date('2016-01-02 23:15:00');
var time = endDate - startDate;
console.log(time/1000/60/60%24); //23.25
like this can calculate the hours span.
var date1 = new Date("2016-01-01 00:00:00");
var date2 = new Date("2016-01-02 23:15:00");
var diff = date2.getTime() - date1.getTime();
var Hrs = diff / (1000 * 60 * 60);
console.log(Hrs);//It's 47.25.
I've tried and got 47.25.
Since there is many solutions to that question I would love to get the shortest and cleanest answer.
var silvester=new Date("2016/01/01 00:00:00");
How can I calculate how many seconds left to that specific date?
var time = (silvester.getTime()-Date.now())/1000;
var silvester=new Date("2016/01/01 00:00:00");
var now = new Date();
var dif = silvester.getTime() - now.getTime()
var Seconds = dif / 1000;
Try this:
(new Date("2016/01/01 00:00:00") / 1000) - (new Date().getTime() / 1000)
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.
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;
How to get timelength from now back to the start of today (00h:00p:00s) in angularjs?
ex: now is 13:45. So timelength = 13*60 + 45 mins
There is no specificity in angular. Just use the Date object.
var date = new Date();
var timelength = date.getMinutes() + date.getHours() * 60;
Get a new JavaScript date object that represents the time now and then use the getHours and getMinutes functions to enable your calculation.
For example:
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var timeLength = hours*60 + minutes;
Converting a javascript date to a number gives you milliseconds since 1/1/1970 (UTC). You can correct for your time zone if you wish, then just take the modulus of the number of milliseconds in a day to get the number of milliseconds since midnight:
var dt = new Date();
var num = dt - dt.getTimezoneOffset() * 60000; // offset is in minutes
var sec = num / 1000; // seconds
var sinceMidnight = sec % (24 * 60 * 60); // seconds since midnight