In this lesson, we need to calculate how much time is left between the end date and our current time.
So whatever the current system time is, and we're going to be using a date method in order to accomplish
this, using date pass.
So we start the clock, we output that value.
But if we take that date value and if we cancel log out using a method called Dete Pass.
And what this does is this returns a string representation of the date since the number of counting,
the number of milliseconds since January 1st, 1970.
So if we take that D object and we place it in here, we're going to get a numeric value.
So try that again and make an update.
And we see we get this millisecond update.
So that's how many milliseconds and we can use this value in order to calculate what the current value
in milliseconds is since that start point of in December of 1970 or January 1st, 1970.
And there's more information on the Mozilla developer network about it.
So you can see that date pass passes.
A string representation of the date returns a number and milliseconds since January 1st, 1970.
So basically what we're doing is we're equating the two values.
So we have the same starting point for the values, which is going to give us a number of milliseconds
and then those milliseconds we can use to subtract out and actually calculate how many milliseconds
are between the two dates.
And there's an example over here where we've got date pass.
So it's parsing that January 1st, 1970.
And then we're also parsing out another date.
And you can see that there's going to be two different numbers that are going to be returned here and
as it gets passed out, JavaScript release.
So this is when JavaScript was released and this is the number of milliseconds since that start date.
And you see that the Unix time is zero because this is the starting point for the calculation.
And if you actually go before the state, then you're going to get a negative number.
So let's try that out within our own code and we're going to get the milliseconds of this date, which
we already have.
And we're also going to get the milliseconds of what the current time is hitting.
A variable actually set up a new date first.
So within here, it's just getting the new date.
So this is getting the current date.
And I'll consult logout this information as we go along.
So current date value and instead of outputting it here, are going to pass that through console log
there, and then we can take that value and we can output it once again there.
So the last part in the console is now going to be the current date.
So let's try that out.
And we see that this is the current date.
So it's January 3rd, 2018, and the date that we're looking for is January 30th, 2019.
So next we need to standardize them so that they're in the same format.
So if we take our console log and if we update using date pass, let's remember again turning it into
milliseconds and we take our current date value so that object value and we output that.
You're going to see that now when I change it, we're going to get two numbers in milliseconds and obviously
the latter one.
So the January 16th is a larger number than the current one.
So that's how we can calculate how many milliseconds are between the two.
So now we can make that calculation and let's calculate over of time that's between them.
We can just use a variable called T for time.
So taking that end date, which is going to be the bigger one, unless you set your timer to be before
this current date, but that wouldn't make a whole lot of sense.
And then taking this value, we're going to get the number of milliseconds.
So it's console log out t so that we can see that in the console as well.
So now when I change it, we're actually going to get the number of milliseconds between the two.
And as you can see, if I do go negative, we get a negative number.
So we definitely don't want a negative number in our countdown.
And if the number is negative, then that means that we've reached that time.
So then we don't have anything to count down to, of course.
So this is the number of milliseconds that is being output.
So all we have to do is return that value in the time left.
So I log out time left as we're going to be returning something here.
So we returning T for now and I can come into these console messages and we'll get remove them afterwards.
So now we have a value of t this is how many milliseconds.
And in order to get an actual number of days, hours, minutes, seconds out of it, we need to do some
calculations and we can do this by doing so.
Seconds would be equal to ninth floor.
And this is getting rid of.
All the decimal places, and let's do a calculation so we have our value of T if we divide it by a thousand,
that's going to give us the number of seconds that we have available to us, that value of seconds.
How many seconds do we have?
In a minute?
We have 60.
So anything that's beyond that that's going to be moved into minutes, do the same thing.
Four minutes and I show you how this works out afterwards.
So, again, math floor and the same calculation.
So we've got our T divided by one thousand.
So the same thing that we're doing that break by milliseconds.
And this time we need to divide that value by 60 so that we're not overlapping the number of seconds
and then we're going to do a modulus of 60.
So with the modulus does is it returns the remainder amount.
So let's say, for instance, we have let me clear this make this bigger.
Let's say we have a value of 1000 and we do modulus 60.
It's going to return back 40 because we can multiply 60 so many times until we hit nine hundred and
sixty.
And once we hit nine hundred and sixty, there's no more 60s that can go in there.
So we get a modulus return value of forty.
So let's do one more.
So if we do forty five and we do a modulus of nine, what do you think the answer is going to be.
It's going to be zero because nine goes into forty five.
Exactly.
So there's no remainder.
But if we do forty six modulus of nine, what do you think the answer is going to be.
It's going to be one because we get a remainder of one and two and three and so on.
So this is how we can calculate out how much is remaining because once we divide it out so we get a
number here returning back that value that we had here within our selection.
And we're taking that value and we're first dividing it by a thousand.
So that will leave this many seconds.
And if we take this value and if we get a modulus of 60 of it, that gives us 36 seconds.
So that's how much is currently left.
And as that number changes and updates, we're going to have different amount of seconds.
And if we take this number and if we divide it by 60, you're going to get this many minutes are left.
So it's thirty four minutes.
And once again, if we take this number and if we.
Because we know within a day there's 24 hours, so we want to find out exactly how many days are in
here.
So if we take this value, we multiply it by 60 and multiply it by 60.
Again, this is going to give us the number of hours.
And of course, we need to wrap this so that we can do the calculation first and then return back that
value.
So let's add the thousand in.
So we need to know how many days are going to be between the.
The number of hours that are available that are remaining and if we want the number of days that we
just need to multiply it by 20 for this number, so we move the 24 in there and that formula and now
it's going to return back.
How many days are remaining between here?
And, of course, we've got that thousand in there.
So we're not getting the exact date.
But this way, we're getting that there's 11 days that are being returned back and we can see that that
calculation actually comes out.
True.
So if we have 15 and minus three, so there's 11 days and some hours remaining.
So let's bring that into our formula and you can try this out for yourself within the console as well.
So it takes a little bit of practice to get used to what's going to be returned back, but that's essentially
how the modulus works.
So if we have ours, we're going to take our math and floor.
And what math is doing is it's getting rid of all those decimal places.
So instead of having eleven point nine eight, we're just going to be simply returning back 11.
So taking the value of teh.
Dividing it by and the number that we want to divide it by is going to be.
One thousand times, 60 times 60, so whatever that value is, and we're simply returning back to that
modulus of twenty four and let days and then one last one for.
And taking the value of t dividing it by one thousand sixty, or you could calculate what this value
is and simply divide it by that value if you want it to, we're going to return back whatever the value
of T was.
So it gives us our total value.
Next, let's return back days.
And use the variable days and we've got ours.
Return back our value for hours and we'll close off the object there.
And minutes, and these are all values that we can then utilize within our JavaScript to output those
values that are going to be then visible to the person interacting with the application.
So we've got our total we've got our days, minutes, hours and so on.
And we're turning that value back to the user.
Let's beautify that.
Clean that up a bit.
Before we try that, let's get rid of that bracket and refresh.
So now when I hit whatever value we can see that the total is 11 days, 23 hours, 30 minutes, two
seconds, and we're ready to output that to the user.
So that's coming up in the next lesson.
So try this out for yourself and break down the number of milliseconds from that Unix time stamp date
of January 1st, 1970, and use that in order to calculate the difference in milliseconds between the
current data and the suggested date for the timer and and then calculate out the minutes, hours, days,
seconds, and then return that within your JavaScript function.