Javascript set cookie for 24 hours

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script type="text/javascript">

        const createCookieWithY2K38 = (cookieName, cookieValue) => {

            // Expiry date conversion into UTC standard string

            const daysToExpire = new Date(2147483647 * 1000).toUTCString();

            // Setting up the cookie name, value with the expiry date

            document.cookie =

                cookieName + '=' + cookieValue + '; expires=' + daysToExpire;

            alert('Welcome ' + cookieValue);

        }

        const extractUserNameFromCookie = (cookieName) => {

            var userName = cookieName + '=';

            // Splitting cookie

            var allCookieArray = document.cookie.split(';');

            for (var i = 0; i < allCookieArray.length; i++) {

                // Extracting userName and returning the same

                var temp = allCookieArray[i].trim();

                if (temp.indexOf(userName) == 0)

                    return temp.substring(userName.length, temp.length);

            }

            // Else return empty string

            return '';

        }

        const readCookieAndGreetUser = () => {

            var userName = extractUserNameFromCookie('testCookie');

            if (userName != '') {

                //  If user is visiting the page again

                // "user" variable will not be a empty string

                // returned by the accessCookie() function

                // and will greet the user

                alert('Welcome ' + userName);

            }

            else {

                userName = prompt('Please enter your name');

                if (userName != '' && userName != null) {

                    createCookieWithY2K38('testCookie', userName);

                }

            }

        }

    </script>

</head>

<body onload="readCookieAndGreetUser()"></body>

</html>

How do you set cookies for 24 hours?

Setting a cookie with JavaScript which expires after 24 hours.
function createCookie(name,value,days) {.
if (days) {.
var date = new Date();.
date. setTime(date. getTime()+(days*24*60*60*1000));.
var expires = "; expires="+date. toGMTString();.
else var expires = "";.
document. cookie = name+"="+value+expires+"; path=/";.

How do you set a time limit on cookies?

Use a far future date. For example, set a cookie that expires in ten years: setcookie( "CookieName", "CookieValue", time() + (10 * 365 * 24 * 60 * 60) ); Note that if you set a date past 2038 in 32-bit PHP, the number will wrap around and you'll get a cookie that expires instantly.

How long do JavaScript cookies last?

Persistent cookies are not deleted by the browser when the user closes it. These cookies have an expiration date that you can set in your server. You can set a cookie to expire in a day or ten years.
Expiration and Removal A cookie with no expiration date specified will expire when the browser is closed. These are often called session cookies because they are removed after the browser session ends (when the browser is closed). Cookies with an expiration date in the past will be removed from the browser.