What is the logic behind the "days * 24* 60* 60* 1000"?

Asked

Viewed 2,429 times

10

Reading some things about cookies, always observe the calculation dias * 24* 60* 60* 1000

What is the logical reasoning behind this?

2 answers

27


It’s just a basic math, how many hours is a day? How many minutes is an hour? How many seconds is a minute? How many thousandths is a second? That’s all it is, finding the number of thousandths of a second of a day without having to memorize a number like 84.600.000 (viu? errei, the right is 86.400.000). In general the numerical precision of time is measured in milliseconds.

  • Got it, thank you very much Maniero!

  • 1

    Or do not need to open the calculator do the calculation and copy to the hehe code.

4

I will use a practical example of the site W3schools

We have the following function, in which we receive the name, value and time in days for the cookie to expire.

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

We took the current date

var d = new Date();

Then the current date is summed up the amount of days in milliseconds, since we need in milliseconds is carried out the calculation days*24*60*60*1000

 d.setTime(d.getTime() + (exdays*24*60*60*1000));

We format the time to expire

var expires = "expires="+ d.toUTCString();

Resulting string "expires=Thu, 01 Jan 1970 00:00:00 UTC"

Then set the cookie

document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

Set to the following string "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"

Then this calculation is done to transform the number of days in milesseconds since the function "setTime" adds values in milesseconds.

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.