Obtain the GMT date for the next day

Need help with code to get the current date and the current date + 1 day (in GMT).

var now = new Date();
var newexp = (now + 3);
var show = newexp.getGMTString();

alert(show);

Goal is to set a cookie to expire in 1 day.

function SetCookie(name, value, exp) {
 var now = new Date();
 var newexp = (now + exp); // exp being # of days before expiration
 document.cookie= name + "=" + value+ "; expires=" + newexp.getGMTString() + ";"
}

SetCookie('name', 'john', '3');

Having issues with the code, need assistance.

Answer №1

The Date object in Javascript allows for simple manipulation of dates stored within it.

To retrieve a Date object for the following day, you can use the following code:

var date = new Date();
date.setDate(date.getDate() + 1);

Answer №2

function UpdateCookie(name, value, exp) {
var current = new Date();
current.setTime(current.getTime()+(exp*24*60*60*1000));
document.cookie= name + "=" + value + "; expires=" + current.toGMTString() + ";"
}

UpdateCookie('name', 'john', '3');

Revised the function to use 'exp' as the factor which represents the number of days for the cookie's expiration.

Answer №3

Consider looking at it from a different perspective by going back to the basic "number of milliseconds since the epoch" concept:

var currentDate = new Date()
currentDate.setTime(currentDate.getTime() + 24 * 60 * 60 * 1000)

Alternatively, you can use two separate variables:

var current = new Date()
var expiration = new Date(current.getTime() + 24 * 60 * 60 * 1000)

Or you can combine it all in one line:

var expiration = new Date(new Date().getTime() + 24 * 60 * 60 * 1000)

Keep in mind that this will give you the date 24 hours from now. This may not be the same as "the same local time tomorrow" which can vary. Personally, I prefer the consistent approach of "a set time length" but your mileage may vary.

If you need the expiration to be a different number of days, simply adjust the constant 24 * 60 * 60 * 1000 (which represents the number of milliseconds in a day) by the desired number of days.

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

Leveraging global variables within Vuex state management strategy

I have successfully added custom global variables into Vue by injecting them. Here is the code snippet: export default function (props, inject) { inject('models', { register(name) { const model = require(`@/models/${name}. ...

What are some ways to display unprocessed image data on a website using JavaScript?

I have an API endpoint that provides image files in raw data format. How can I display this image data on a website using the img tag or CSS background-image property, without utilizing canvas? One possible approach is shown below: $.get({ url: '/ ...

Showing the information obtained from an API request once the user submits the form without the need to reload the page

I am currently working on a form that will take search query requests from users upon submission and then display the results by making an API call. My goal is to show these results without the page having to refresh, using AJAX. The backend connection to ...

The setInterval function is active in various components within Angular 6

Recently, I started using Angular(6) and incorporated the setInterval function within a component. It's functioning properly; however, even after navigating to another route, the setInterval continues to run. Can someone help me determine why this is ...

Having difficulty uploading an image to Facebook through the graph API

I have a requirement to upload a photo to Facebook using the Javascript SDK, but I am experiencing some difficulties: Firstly, FB.login(function (response) { if (response.authResponse) { va ...

What is the reason behind observing numerous post requests in the Firebug console after submitting a form through Ajax in jQuery?

I have integrated the jquery Form plugin for form submission and everything seems to be functioning properly. However, upon turning on the firebug console and clicking the submit button, I notice that there are 10 post requests being sent with the same da ...

MUI: Interaction with a button inside a MenuItem when not interacted with MenuItem itself?

Currently, I am utilizing MUI's Menu / MenuItem to create a menu of missions / tasks similar to the screenshot below: https://i.sstatic.net/6FGrx.png The MenuItem is interactive: // ... other code <MenuItem value={mission.issu ...

Ways to retrieve the related file in Angular service files?

I am looking to retrieve an array from another service file (chart-serv.js) in my return-serv.js service file using AngularJS. How can I reference the dependent file enclosed within double braces [ ], in line 1? return-serv.js var app = angular.module(& ...

Avoiding the selection of HTML canvas objects

I am currently working on customizing my homepage with an interactive animation. However, I am facing some challenges in integrating it seamlessly into the page. You can view the progress at . My main issue is preventing the canvas object from being select ...

Accessing information from Next.js API endpoint in a Next.js web application

Currently, I am in the process of developing a web application using Next.js APP Router for both the frontend and backend components. The frontend takes care of rendering the user interface, while the backend comprises API routes. I require some guidance o ...

Identify the quantity of dynamically added <li> elements within the <ul> using jQuery

I'm facing an issue where I need to dynamically add a list of LI items to a UL using jQuery. However, when I try to alert the number of LI elements in this list, it only shows 0. I suspect that it's because the code is trying to count the origina ...

The conversion from a string to a date type for the property fromDate was unsuccessful

I am struggling to save Date type in the format of dd/mm/yyyy, as my current format is mm/dd/yyyy. Here is my jsp Code: <form:input path="fromDate" id="fromDate" /> <form:errors path="fromDate"/> Despite using a script with the date format & ...

Encountering the error message "Uncaught Promise (SyntaxError): Unexpected end of JSON input"

Below is the code snippet I am using: const userIds: string[] = [ // Squall '226618912320520192', // Tofu '249855890381996032', // Alex '343201768668266496', // Jeremy '75468123623614066 ...

Concealing specific HTML elements with ng-view in AngularJS

I recently started a project in AngularJS and I'm utilizing ng-view with $routeProvider for templating. However, I've encountered a minor issue where I don't want the navbar to display on specific pages like the landing, login, and registrat ...

Pressing the reset button will restore the table to its original

As a new React developer with experience mainly in hooks, I have been struggling to find a good example involving hooks. Currently, I am working on implementing an antd table with search functionality. My question is, when a user types something into the ...

Having trouble retrieving a value from a .JSON file (likely related to a path issue)

My React component is connected to an API that returns data: class Item extends Component { constructor(props) { super(props); this.state = { output: {} } } componentDidMount() { fetch('http://localhost:3005/products/157963') ...

Understanding the process of verifying signatures with Cloud KMS

I've been struggling to confirm the validity of a signature generated using Google's cloud KMS, as I'm consistently receiving invalid responses. Here's my approach to testing it: const versionName = client.cryptoKeyVersionPath( p ...

I am attempting to display only the description of the button that I click on

How can I ensure that only the container pressed will display its description when the description button is clicked, instead of all mapped containers showing their descriptions? `import { useState } from "react"; export default function Se ...

Explore button that gradually decreases max-height

I have a "Show More" button that expands a div by removing the css attribute max-height, but I want to add an animation similar to jQuery's slideToggle() function to smoothly reveal the rest of the content. This is the code I am using: <div id="P ...

Why isn't the close button in jQuery working on an Asp.net page?

My current project involves working with Asp.net and C#. I am facing an issue where I click on a button to display a message in a popup box, but when I try to close the popup by clicking the close button, it does not close. Here is the syntax for the aspx ...