javascript hours to real hours - javascript

I have a piece of code like this:
window.setInterval("reloadIFrame();", 3000);
^
*
I want to know if there is a chart anywhere that can translate js time (*) to real hours, like one hour 2 hour three hour is there any way?

That 3000 is just expressed in milliseconds, so standard math will do.
3000ms = 3000ms / 1000ms/s / 3600s/h = .00083 hours

The parameter does not accept hours, you would have to multiply to get it in millisecond.
1000 ms = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
2 hours -> 60 min * 2 hours = 120 minutes * 60 = 7,200 seconds * 1000 = 7,200,000 ms
The reverse would be a division.
3000 ms / 1000 = 3 seconds / 60 = 0.05 minute / 60 = 0.00083 hours

Related

Count down timer in coming soon page in javascript

I am trying to code a count down timer in Vanilla Javascript. Below is the code:-
var futureDate = new Date("oct 31,2021 10:00:00").getTime();
var currentDate = new Date().getTime();
var diffTime = futureDate - currentDate;
console.log(diffTime);
var days = Math.floor(diffTime / (1000* 24* 60*60));
console.log(days);
var hours = Math.floor(diffTime / (1000 * 60 * 60));
console.log(hours);
var minutes = Math.floor(diffTime / (1000 * 60));
console.log(minutes);
var seconds = Math.floor(diffTime / (1000));
console.log(seconds);
Below is the output of the code provided in the console.
This output is in accordance with below image
But I am failing to understand the particular code written in w3schools as highlighted in blue color in the image below
I am not able to figure out the difference between 2 code written?
Date.getTime() returns the number of milliseconds since the Unix epoch (1900-01-01 00:00:00).
When you subtract the time between 2 dates, you will get the time difference in milliseconds. In your code, you are showing
The total number of hours between the dates
The total number of minutes between the dates
The total number of seconds between the dates
Let's take the number of hours as an example, the code written in w3schools takes the remainder from dividing the time difference by the number of milliseconds per day. This way, you will get the time difference in less than 24 hours and you can use it to calculate the time difference in hours.
Do the same for the minutes and seconds and you will get the same result written in w3schools
var futureDate = new Date( "oct 31,2021 10:00:00" ).getTime();
var currentDate = new Date().getTime();
var timeDiffInMilliseconds = futureDate - currentDate;
var millisecondsPerSecond = 1000;
var millisecondsPerMinute = millisecondsPerSecond * 60; // 1000 * 60
var millisecondsPerHour = millisecondsPerMinute * 60; // 1000 * 60 * 60
var millisecondsPerDay = millisecondsPerHour * 24; // 1000 * 60 * 60 * 24
console.log( 'timeDiffInMilliseconds: ' + timeDiffInMilliseconds );
var days = Math.floor( timeDiffInMilliseconds / millisecondsPerDay );
console.log( 'days: ' + days );
var timeDiffInLessThan1Day = timeDiffInMilliseconds % millisecondsPerDay;
var hours = Math.floor( timeDiffInLessThan1Day / millisecondsPerHour );
console.log( 'hours: ' + hours );
var timeDiffInLessThan1Hour = timeDiffInMilliseconds % millisecondsPerHour;
var minutes = Math.floor( timeDiffInLessThan1Hour / millisecondsPerMinute );
console.log( 'minutes: ' + minutes );
var timeDiffInLessThan1Minute = timeDiffInMilliseconds % millisecondsPerMinute;
var seconds = Math.floor( timeDiffInLessThan1Minute / millisecondsPerSecond );
console.log( 'seconds: ' + seconds );
Okay, first you need to understand the basic concept of dates in vanilla javascript.
1 second = 1000 milliseconds
1 minute = 60 seconds
1 hour = 60 minutes
1 day = 24 hours
Math.floor is simply used for returning the highest rounded value possible.
Now coming to your question,
var distance = countDownDate - now;
This line basically takes two UNIX format date integers and gives you the difference in the form of unix timestamp. So once you get the difference you need to find out how many hours, minutes and days it comprises of, and that's just it.
We use the simple formulas I mentioned to top to identify just that.
1000 * 60 * 60 * 24 is 1 day
So we divide the total difference in two dates by the 1 day to get the number of days in total with the following line:
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
Similarly, with the code below, we take the difference in two dates and first get the number of days from it and then whatever time remains we use that time to further calculate the hours by dividing the value by (1000 * 60 * 60).
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
The following line first calculates the number of hours from the difference and then whatever remains is then further divided by a minute to get the totals minutes remaining. Similarly, concept will be applied to calculate the seconds.
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));

Convert seconds to HH:MM:SS:MS and/or HH:MM:SS;F

I'm trying to write a logic to convert seconds to the following formats:
HH:MM:SS:MS, where MS is milliseconds
HH:MM:SS;F, where F are the frames
(and not just to HH:MM:SS, therefore this question is different from the others on Stackoverflow)
I have the following logic for getting the HH:MM:SS format currently:
getTime(seconds) {
let h = Math.floor(seconds / 3600);
seconds = seconds % 3600;
let min = Math.floor(seconds / 60);
seconds = Math.floor(seconds % 60);
if (seconds.toString().length < 2) seconds = "0" + seconds;
if (min.toString().length < 2) min = "0" + min;
return (h + ":" + min + ":" + seconds);
}
but how can I get milliseconds or frames?
If seconds is a float, you can take Math.round((seconds - Math.floor(seconds)) * 1000) to get remaining milliseconds. Or Math.round((seconds - Math.floor(seconds)) * fps) where fps is the number of frames per second.
If Your function only takes seconds, then there is no way to get milliseconds out of this information...
You can assume that it is zero milliseconds.
If you want to be accurate to milliseconds, your function should take milliseconds.
var getStringFromMS=(ms,res=[])=>(
[1000,60,60,24].reduce((rest,curr,i)=>(
res[3-i]=rest%curr,Math.floor(rest/curr)
),ms),res.join(":")
);
In action with current time
Simply iterate over the different periods and reduce the milliseconds to days, while doing that the result is stored in res, which can be joined then simply. You may replace 1000 with frames

javascript time conversion (seconds-=)

time -= 50 * 60
I am unsure why time-= is used in the above code snippet? What is the purpose?
Hours is equal to the number of hours in the number of seconds rounded down to a whole number. This difference between hours and the precise number of seconds contains an amount between 0 and just below the maximum number of seconds in 1 hour. To get this, the time in Hours is subtracted from the number of seconds. A similar process follows for the number of minutes.
I will explain line by line to help you get this point:
var seconds = count; //25 * 60 = 1500 (1)
=> just get total seconds before calculating
var hours = Math.floor(seconds / 3600);
=> this is how to calculate hour
seconds -= hours * 3600;
=> this code can be written is easy way seconds = seconds - (hours * 3600);
so the result seconds in this line is the remain second after calculating hours. Now if you get this point the remain code is easily to understand.
var minutes = Math.floor(seconds / 60);
seconds -= minutes * 60
Now, after running this code you can check the result by:
var total_seconds = hours*3600 + minutes*60 + seconds;
The result total_seconds must be equal to value of seconds in the first line of code (1).
This is the basic of programming. If you cannot understand, try to debug it by console.log() to show result. Try yourself is the good way to improve your skill.
var seconds = 7510;
console.log("seconds: "+seconds);
var hours = Math.floor(seconds / 3600);
seconds -= hours * 3600;
console.log("hour: "+hours);
console.log("seconds after calculating hours: "+seconds);
var minutes = Math.floor(seconds / 60);
seconds -= minutes * 60;
console.log("minutes: "+minutes);
console.log("seconds after calculating munites: "+seconds);
var total_seconds = hours*3600 + minutes*60 + seconds;
console.log("total_seconds: "+total_seconds);

JavaScript (Jquery) Decimal Point Removal

Currently I am working on JavaScript, jQuery, HTML5 to improve myself. I have a opensourcely coded clock, which I have converted it into a counter (reverse counter).
Problem I am having is, in my setInterval(){...} I have four variables -> second,min,hour, and day. The problem is, when I get the seconds, I get something like 1.155, 2.312, 3.412 (seconds).
My setInterval function is below
setInterval(function(){
//var duration = parseInt(Date.now() /1000 ) - 1365470000;
var futureTime = Date.parse('April 10, 2013 22:00:00');
var duration = (( parseInt(Date.now() - futureTime ) / 1000));
var seconds = duration % 60;
duration = parseInt(duration / 60);
var minutes = duration % 60;
duration = parseInt(duration / 60);
var hours = (duration)%24;
duration = parseInt(duration / 24);
var days = duration % 365;
animation(gVars.green, seconds, 60);
animation(gVars.blue, minutes, 60);
animation(gVars.orange, hours, 24);
animation(gVars.red, days, 365);
},1000);
}
And my output is below for some random time since i use parseInt(Date.now()).
I have to give the link since I don't have enough rep.
http://i.stack.imgur.com/0Zkbi.png
How can I get rid of the decimal point in setInterval(){} functions?
Thanks in advance.
JavaScript offers more convinient API to work with date and time in order to fetch seconds, minutes, hours and days. Try this code:
var duration,
seconds,
minutes,
hours;
duration = new Date((new Date('April 11, 2013 23:00:00')) - (new Date()));
seconds = duration.getSeconds();
minutes = duration.getMinutes();
hours = duration.getHours();
Now you will have integer values in all 4 variables above, without any decimal point.
var seconds = 1234.13;
var seconds = seconds + '';
seconds = seconds.split('.')[0];
console.log(seconds);

Some explanations on Date.now

Could someone explain to me what this returning number means? and how it is derived to that?
console.log(Date.now() - 24 * 60 * 60 * 1000);
If I wanted to use the above formula to display the next 15minutes and not 24 hours? how would I alter it?
Date.now() returns:
the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
24 * 60 * 60 * 1000 in milliseconds represents 24 hours*. So you basically get a timestamp 24 hours in the past from now. Notice that due to DST this doesn't necessarily compute a timestamp one day in the past. It's 24 hours in the past.
Also to get some meaningful output you should wrap resulting number in Date:
console.log(new Date(Date.now() - 24 * 60 * 60 * 1000));
Finally Date.now() can be replaced with new Date() when using in arithmetic expression.
* - 24 (hours) times 60 (minutes in hour) times 60 (seconds in minute) times 1000 milliseconds in second.

Categories