What is the function of date.getTime when used in the expires parameter of a cookie?

I've come across a snippet of code that I'm trying to decipher. It's used to set a cookie with an expiry date, and it looks something like this.

var date = new Date();
date.setTime(date.getTime() + 1000*60*60*24*365);
var expires = "; expires=" + date.toGMTString();

My goal is to understand what each number in the code represents. While I know that it involves adding milliseconds to the time object, breaking down the significance of each numeric value is where I'm currently stuck.

Answer №1

There are 1000 milliseconds in a second

60 seconds in a minute

60 minutes in an hour

24 hours in a day

365 days in a year

By multiplying these values together, you can calculate the total number of milliseconds in a year.

Answer №2

let currentDate = new Date(); // creating a date object for the current time

currentDate.setTime( // updating the time
currentDate.getTime() // representing now in milliseconds since 1970

+ 1000 // adding milliseconds in a second
* 60 // multiplying to get seconds in a minute
* 60 // getting minutes in an hour
* 24 // calculating hours in a day
* 365 // approximating days in a year. The total is the number of milliseconds in a year
);
const expirationDate = "; expires=" + 
currentDate.toGMTString(); // formatting the time as required for cookies

Please refer to:

for further details and explanations.

Answer №3

If you take 1000 milliseconds and multiply it by 60 seconds, then by 60 minutes, followed by 24 hours, and finally by 365 days, you will get the equivalent of a year.

Answer №4

Consider this perspective:

1000*60*60*24*365

1000 // Changing milliseconds to seconds
60 // Converting seconds to minutes
60 // Transforming minutes to hours
24 // Converting hours to days
365 // Changing days to years

Answer №5

There are 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day, 365 days in a year,

This totals the number of milliseconds in a year, essentially fast-forwarding time by one year

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges when working with AJAX/jQuery in terms of fetching JSON data and setting dataType

I am currently facing a challenge with my practice of AJAX/jQuery coding. Despite my efforts to learn and improve, I find the concepts of jQuery and AJAX quite perplexing. Specifically, I am struggling to understand dataTypes and how to manage different ty ...

The process of overriding CSS applied through JavaScript

I am using a third-party multi-select dropdown. Similar to Select2, the multi-select dropdown is created using JQuery with select2.min.js and the width of the dropdown is automatically calculated. Is there any way to apply static width to it, as I believe ...

Curious about retrieving a value in a Bootstrap confirm modal?

Although it may seem like a silly question, I am struggling to figure out how to wait for the confirmation button to return a value in Javascript. Since Javascript is single-threaded, I need to implement a callback function that will wait until a button is ...

Construct a new array within a JavaScript constructor function

I have been working on setting up a constructor and trying to initialize an array inside the object that will be created. This specific array is meant to hold multiple objects. function Cluster3DObject(name){ this.name = name; ...

Having trouble with an unknown cipher when using the crypto module?

Encountering difficulty encrypting with aes-256 using cipher - getting an error stating unknown cipher. Any insights on what may be missing in the code below? index.js function encryptPaymentId(specialtyID, paymentId) { const convertToStrin ...

jQuery encounters a 400 error while attempting to make a GET request

I have been using a jQuery ajax GET request that sends a clientId to the server. If the clientId is invalid, the server returns a 400 "Bad token etc..." error message. While this setup works fine, I am puzzled by why jQuery displays this error in the conso ...

Ensuring all fetch requests are completed successfully before dispatching an action in React-Redux

I'm currently developing a React application and utilizing Redux to manage the state. Here is a snippet of the code: category-arrows.component.jsx: import React, { Component } from 'react'; import { connect } from 'react-redux'; i ...

The v-model for a particular field is not reflecting real-time updates like the other fields with the same datatype. I'm trying to figure out what could be causing this issue

In my specific model, there are various values that can be updated through a form on the webpage. These values include "Quantity", "Rate", "Amount", "Net rate", and more. The problem I am facing is with binding these values with my inputs using v-model. E ...

The art of breathing life into UpdatePanels

Although I have experimented with the UpdatePanelAnimationExtender from the Ajax Control Toolkit, my main issue with it is that it does not wait for the animation to finish before loading new content. What I aspire to achieve is: Commence asynchronous r ...

Ways to set values for an array of objects containing identical key names

let people = [ { firstName: 'Ben' }, {firstName : 'Bob' } ]; let location = { city: 'Dublin' , Country: 'Ireland' } ; let result = []; let tempObj = {}; for(let person of people){ tempObj = Object.assign({}, ...

Bugfender is reporting a ReferenceError stating that it can't locate the variable BroadcastChannel

While Bugfender works well in my Vue.js application on Chrome, I am experiencing issues with it on my Safari Mac. Specifically, I am encountering an error in the browser console that says "ReferenceError: Can't find variable: BroadcastChannel". This i ...

Pass data from Wordpress plugin to JavaScript function

I recently developed a plugin for a WordPress website. Inside the plugin, I am trying to call a JavaScript function (passing a string as a parameter) from a file that I have just enqueued. However, nothing seems to be happening. Can anyone help me identify ...

Utilizing OrbitControls with Socket.io in a Three.js Sphere

I'm currently troubleshooting an issue with my project. I have a three.js sphere ("HTML1" on Desktop) with controls (THREE.OrbitControls) that is functioning properly on both my computer and tablet. My goal is to control this sphere through my iPad, s ...

Tips for reversing the order of a v-for loop using Vue.js

I am working with an array called "names" that starts off empty names: [] To add elements to this array using the unshift() function, which adds elements to the beginning instead of the end, I do it like this: names.unshift("Leonardo") names.unshift("Vict ...

The utilization of ng-class in a dropdown menu: A step-by-step guide

I am new to AngularJS and I am trying to figure out how to disable a selectlist when an option has been chosen and enable it when no option has been selected. My goal is to have the selectlist disabled if an object is not empty, and enabled if it's em ...

The javascript code appears to be functioning properly on desktop browsers but is not functioning as expected on Chrome's mobile browser

On my WordPress site, I have created a Java Bootstrap loan calculator that works perfectly on desktop. However, when I use Chrome on mobile, it is not functioning properly. I tried using the wp-coder plugin and also manually added the necessary code. Int ...

Animating shakes with AngularJS

As I continue learning AngularJS, I find myself transitioning from using jQuery to achieve similar functionality. One specific challenge I'm facing involves my login function: it currently redirects a user upon successful login, but only prints to the ...

Design personalized social media icons that allow users to easily share the link of the current page

Currently, I am working on creating a widget that includes social sharing buttons. The goal is for these buttons to share the URL of the current page when clicked on various social media platforms. For instance, I attempted to integrate a Twitter share but ...

An error of TypeError is being encountered in AngularJS

Hello, I am new to creating mobile apps with Ionic and Angular. Currently, I have a simple login form and a corresponding login button that triggers a controller. Here is the code snippet for the controller: angular.module('starter.controllers', ...

There is no $key present within an AngularFireDatabase.object

I have encountered an issue in my Angular application while attempting to update records on a Firebase database. I am utilizing AngularFireDatabase to bind the list initially, which is then intended to be used for updating specific records. However, the ch ...