To create a new cookie with an expiration date

PHOTO EMBED

Mon Jul 11 2022 15:23:13 GMT+0000 (Coordinated Universal Time)

Saved by @latte26 #javascript

document.cookie = ‘name=Buffy; expires=Fri, 15 Dec 2020 23:00:00 GMT’;

let myCookies = document.cookie;

This command returns all cookies, meaning you’d have to manually extract the data you need.

The code for updating a cookie is similar to the code for creating a new one—you simply overwrite the existing cookie with a new value. Let’s change the user’s name from “Buffy” to “Sabrina”:

document.cookie = 'name='Sabrina'; expires=Fri, 15 Dec 2020 23:00:00 GMT';
To delete a cookie, you do the same thing again. This time, however, you should set expires to be a date in the past:

document.cookie = 'name='Sabrina'; expires=Fri, 15 Dec 1970 10:00:00 GMT';
content_copyCOPY

https://github.com/js-cookie/js-cookie/tree/latest#readme