In JS i've a var {double} hour, where i want to know how many 15 minutes are in there.
// Example
var hour = 2.30
// Example calculation
// 1 hour = 60*60 = 3600
// 2 hour = 60*120 = 7200
// 15 min = 60 * 15 = 900
// 30 min = 60 * 30 = 1800
// count 15min within hour
var result = 7200 + 1800 = 9000 / 900 = 10
I can count the hours like this
var hour = 2.30
var tmp_hour = parseInt((hour.toString().split('.')[0]) * 60 * 60);
// tmp_hour = 7200
But i can't find a way to calculate the fractioanal part to minutes, see code
// this only working with::
// hour = 2.15, 2.45
// hour 2.15 => result => 900 (correct)
// hour 2.45 => result => 2700 (correct)
// hour 2.30 => result => 180 (not correct)
var tmp_min = parseInt(hour.toString().split('.')[1] / 15 * 900);
Does someone nows a better way for calculating this.
And i've another question whats could be offtopic.
I have a table with a couple of cells. When i click on a cell its get divided into four pieces (time interval, where every piece stands for 15 min).
// example
<table>
<tr>
<td></td>
<td></td>
<td>
<div class="divider">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</td>
<td></td>
</tr>
</table>
This is working fine, but i want to calculate the width for every label (4 pieces).
For instance if the clicked cell has a width of 17 px.
I want to following width
// span 1: 4px
// span 2: 5px
// span 3: 4px
// span 4: 4px
// total: 17px
I've tried several calculation with mod and dividing but could not get the disired result.
Related
I have a few doubts about following the code. can anyone please help me to understand the below code?
async function getNewDate(xlSerial) {
// milliseconds since 1899-31-12T00:00:00Z, corresponds to xl serial 0.
let xlSerialOffset = -2209075200000;
let elapsedDays;
// each serial up to 60 corresponds to a valid calendar date.
// serial 60 is 1900-02-29. This date does not exist on the calendar.
// we choose to interpret serial 60 (as well as 61) both as 1900-03-01
// so, if the serial is 61 or over, we have to subtract 1.
if (xlSerial < 61) {
elapsedDays = xlSerial;
}
else {
elapsedDays = xlSerial - 1;
}
// javascript dates ignore leap seconds
// each day corresponds to a fixed number of milliseconds:
// 24 hrs * 60 mins * 60 s * 1000 ms
let millisPerDay = 86400000;
let jsTimestamp = xlSerialOffset + elapsedDays * millisPerDay;
return new Date(jsTimestamp);
}
Here in the code can anyone tell me what is let xlSerialOffset = -2209075200000;
2)why do subtract 1 in second condition conditions?
I'm doing a count down with moment.js and duration to return days, hours, minutes and seconds. So, when I use moment.duration with miliseconds, it return a non-sense days...
var miliseconds = 4423852000
var divided = miliseconds / 1000 / 24 / 3600 // return: 51,20 days (correct)
var duration = moment.duration(miliseconds).days() // return: 20 days (???)
document.querySelector('.days').innerHTML = divided
document.querySelector('.result').innerHTML = duration
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.js"></script>
<pre class="days"></pre>
<pre class="result"></pre>
I'm using moment.duration correctly??
You should use asDays() instead of day():
As with the other getters for durations, moment.duration().days() gets the days (0 - 30).
moment.duration().asDays() gets the length of the duration in days.
var miliseconds = 4423852000
var divided = miliseconds / 1000 / 24 / 3600
var duration = moment.duration(miliseconds).asDays()
document.querySelector('.days').innerHTML = divided
document.querySelector('.result').innerHTML = duration
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.js"></script>
<pre class="days"></pre>
<pre class="result"></pre>
I have a problem with javascript. I'm pretty new, so I appreciate your help in advance.
I have a form in which I have a date in hh: mm: ss format and to which through an input field I would like to increase or decrease the hh: mm: ss by percentage. The percentage can be integer or decimal number.
That is, in one hour "example".
17:22:14 I would like to increase by 6% or if it is 14.3% decimal, or else the same value but in negative, i. e. decrease by 6% or 14.3%.
I have an example to show the percentage of an increase from a given hour but I can't do what I want to do.
With this example in PHP I would know the percentage from a date, but I don't want this, I want to know how to increase or decrease in percentage from a given time and in javascript.
<?php
$date = '08:53:34';
$parts = explode(':', $date);
$secs = $parts[0] * 3600 + $parts[1] * 60 + $parts[2];
// day has 86 400 seconds
echo $secs / 864 . '%'; // return 37.05% (you can round it)
?>
And in javascript I have this example, but the other way around. What I want is to increase or decrease not knowing the increased time from a given hour.
var time_begin = '08:00:00';
var a = time_begin.split(':');
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var start_time = Math.round((seconds/(24*60*60))*100);
Thanks in advance
You can use for solve your problem standart JS object Date();
Algorithm is easy:
1) Make Date object of current or needed date. (today variable in my code)
2) Make Date of ending date, percentage to this date we'll looking
3) Just looking for difference between today date and tomorrow
4) Now we received percent that remained until tomorrow(Or other date).
5) If your need passed percents just use 100 - ((tomorrow - today) / ONE_DAY * 100) in last varibale, before use result
6) For increasing/decreasing use example.
Example is here:
const ONE_DAY = 8.64e7; // 86400000 milliseconds
let time = "08:54:33"; // Your time where your looking %
let today = new Date();
today.setHours(time.split(":")[0], time.split(":")[1], time.split(":")[2]); // Set time from what we will looking %
let tomorrow = new Date(Date.now() + ONE_DAY).setHours("0", "0", "0");
let percent = ((tomorrow - today) / ONE_DAY * 100).toFixed("2") + "%";
// it's for increase/decrease
const HOUR = 3.6e6; // 360000
const MINUTE = 6e4; // 60000
const SECOND = 1e3; // 1000
let incPercent = 0.03; // it's mean 3%
let increased = new Date(today.getTime() + (incPercent * HOUR));
I am trying to generate time slots with a gap of 15min between each one, like the following :
["15:30", "15:45", "16:00", "16:15"...]
So far, I managed to make it. However, if the current time is 15:25 (just an example) the generated array will start from 15:30 what I need instead (in this case) to generate time slots starting from 16:00 meaning that only the first time slot should be approximately away 30 min from the current time.
Currently, I have the following code :
//Used momentJS library
function getTimeStops(end) {
var roundedUp, startTime, endTime, timeStops;
roundedUp = Math.ceil(moment().utc().minute() / 30) * 30;
startTime = moment().utc().set({
minute: roundedUp
});
endTime = moment(end, 'HH:mm');
if (endTime.isBefore(startTime)) {
endTime.add(1, 'day');
}
timeStops = [];
while (startTime <= endTime) {
timeStops.push(new moment(startTime).format('HH:mm'));
startTime.add(15, 'minutes');
}
return timeStops;
}
var timeStops = getTimeStops('02:00');
console.log('timeStops ', timeStops);
You're rounding to the nearest half hour here:
roundedUp = Math.ceil(moment().utc().minute() / 30) * 30;
Round to the nearest hour instead:
roundedUp = Math.ceil(moment().utc().minute() / 60) * 60;
Edit: just realised that the above is wrong and doesn't actually answer your question.
You do need to change your roundedUp value though.
What you need to do is:
Add 30 minutes to the current time
Round it to the closest 15 minute interval
That way - at most - you'll be 7.5 minutes out.
So for step 1, add 30 minutes
var add30mins = moment.utc().add(30, 'minutes')
Now we need to find the closest 15 minute interval.
var roundTo15Mins = Math.round(add30Mins.minute() / 15) * 15;
Then you can plug that into your startTime.
HTH
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