What is the difference between two times? βοΈ

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

π£ Chris, make a speedtest report of this function! Uhh, Ok! No problem but shit my math is not good.
...
Opens up Time diff website -> Pastes two times -> reports back.
It might be just my math being so bad, but let's make a simple real-world tool that will give us the time past two times.
It will end up looking like this Codepen.
<div class="container">
<div class="dates">
<div class="start">
<i>Start time:</i> <br />
<input type="number" min="0" max="24" id="s_h" placeholder="HH" />
<input type="number" min="0" max="60" id="s_m" placeholder="MM" />
<input type="number" min="0" max="60" id="s_s" placeholder="SS" />
</div>
<div class="space"></div>
<div class="end">
<i>End time:</i> <br />
<input type="number" min="0" max="24" id="e_h" placeholder="HH" />
<input type="number" min="0" max="60" id="e_m" placeholder="MM" />
<input type="number" min="0" max="60" id="e_s" placeholder="SS" />
</div>
</div>
<button id="button">Perform some magic β¨</button>
<div id="output"></div>
</div>
We need two divs that will each keep three inputs for the hours, minutes, and seconds. Then we need a button to perform our magic on click, and a output div to place the result in!
Let's make the above show a bit nicer:
.container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
background: #4ea8de;
}
.dates {
display: flex;
text-align: center;
}
.dates i {
margin-bottom: 10px;
}
.dates input {
width: 40px;
padding: 5px;
margin-top: 10px;
}
.space {
margin: 0px 10px;
}
button {
margin-top: 20px;
font-size: 24px;
padding: 10px 20px;
background: #7400b8;
border-radius: 5px;
color: #80ffdb;
cursor: pointer;
transition: all 0.5s ease;
}
button:hover {
background: #6930c3;
}
#output {
margin-top: 20px;
font-size: 18px;
}
I'm mainly using flexbox to center some elements and adding some margins and colors to make it look better.
Note that we are adding a transition to the button; this will make the button background easily fade instead of a hard hover effect. Another cool transition is this Hamburger menu.
On to the magic part, JavaScript is what is going to make everything work.
First, we need to define all our variables
const startHour = document.getElementById('s_h'),
startMinute = document.getElementById('s_m'),
startSecond = document.getElementById('s_s'),
endHour = document.getElementById('e_h'),
endMinute = document.getElementById('e_m'),
endSecond = document.getElementById('e_s'),
button = document.getElementById('button'),
output = document.getElementById('output');
As you can see above, we can add one row of variables, just beats writing const or var every time. You can space them out with comma's.
Now let's add a click event to our button:
button.addEventListener('click', function() {
// Code coming here
// π
});
Awesome, now we need to get our start and end date inside this click:
let startDate = new Date(
2020,
05,
05,
startHour.value,
startMinute.value,
startSecond.value
);
let endDate = new Date(2020, 05, 05, endHour.value, endMinute.value, endSecond.value);
We just define a random day, we only using the time settings in this example.
Now let's get the difference between these two times!
let difference = endDate.getTime() - startDate.getTime();
The getTime() function returns the timestamp, which makes it easier to calculate with
Let's first check if the end date is not in the future:
if (difference < 0) {
output.innerHTML = 'Wow dude, you just went back to the future! π½';
} else {
// Code coming below
// π
}
So we are giving the user some feedback if the is trying to scam us, the tug! π
difference = difference / 1000;
let hourDifference = Math.floor(difference / 3600);
difference -= hourDifference * 3600;
let minuteDifference = Math.floor(difference / 60);
difference -= minuteDifference * 60;
output.innerHTML = `${hourDifference} hours, ${minuteDifference} minutes, ${difference} seconds`;
Wow, hold your horses, what's going on here? π€
First, we need to divide the difference by 1000. This removes the milliseconds.
Then we say give us the hours past in the difference, 3600 = (60 seconds * 60 minutes = 1 hour). We are using Math.floor to always round downwards. We don't want 0.9 hours to become 1.
Next, we need to detract the passed hours from the difference.
We do the same for the minutes, but we only need to divide by 60, since we detracted the hours already. Then again, we remove whatever minutes passed.
The difference we end up with is the seconds!
Then we use some liquid template tags (${variable}) to return the result to the user!
Voila! We are now masters of time and universe π§ββοΈ!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter