In web development, cookies are used to store small pieces of data on the client side. The expression 24 * 60 * 60 * 1000
is commonly used to set the expiration time of a cookie. This calculation converts days into milliseconds (24 hours * 60 minutes * 60 seconds * 1000 milliseconds), which is the format required by the maxAge
attribute in JavaScript. Understanding this keyword is crucial for managing cookie lifespans effectively, ensuring data is stored for the appropriate duration and enhancing user experience.
Here’s the breakdown:
So, days * 24 * 60 * 60 * 1000
calculates the total milliseconds for the specified number of days, which is used to set the cookie’s expiration time.
The expression 24 * 60 * 60 * 1000
is used to set the duration of a cookie in milliseconds. Here’s how it breaks down:
24
hours60
minutes per hour60
seconds per minute1000
milliseconds per secondSo, 24 * 60 * 60 * 1000
equals the number of milliseconds in one day.
Setting a cookie in JavaScript:
function setCookie(name, value, days) {
const d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
Creating a cookie in Node.js:
res.cookie('jwt', 'tokenValue', {
httpOnly: true,
sameSite: "None",
secure: true,
maxAge: 24 * 60 * 60 * 1000 // 1 day
});
These examples show how to use the 24 * 60 * 60 * 1000
expression to set cookie durations in both client-side and server-side JavaScript.
Using 24 * 60 * 60 * 1000
in a cookie’s expiration setting converts days into milliseconds. Here’s the breakdown:
Multiplying these together gives the total number of milliseconds in a day. This method allows developers to set cookie expiration times precisely, ensuring cookies expire exactly after the specified number of days.
The expression days * 24 * 60 * 60 * 1000
is used to convert days into milliseconds, which is the format required by the maxAge attribute in JavaScript.
It calculates the total milliseconds for the specified number of days, allowing developers to set cookie expiration times precisely and ensuring cookies expire exactly after the specified number of days.