Using variables in Javascript to manipulate the Date object

Here is my JavaScript code snippet:


var earlierdate = new Date(2012, 09, 22);
alert(earlierdate.getDay());

var date2 = new Date('2012, 09, 22');
alert(date2.getDay());

The issue I am facing is that the first alert displays 1 or Monday (incorrect) while the second one displays 6 or Saturday (correct). When I try to use variables instead of hard-coded values like this:


var date1 = new Date(a, b, c);
alert(date1.getDay());

I am unable to figure out the correct syntax. I have tried several variations without success.

Any help would be greatly appreciated. Thank you!

Answer №1

The parameter month in the context of Date starts counting from 0.

month

This is an integer value that represents the month, with January as 0 and December as 11.

So if you are referring to September, your code should look like this:

var earlierdate=new Date(2012, 8, 22);

Answer №2

//Method 1
var currentDate = new Date();
currentDate.setFullYear(2010, 0, 14);

//Method 2 (Simpler)
var currentDate = new Date(2010, 0, 14);

Using either of these methods will update the date to January 14th, 2010.

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

Safari causing margin problems

Encountering a margin problem specifically with Safari that I'm having trouble debugging. While everything functions properly in Firefox, Chrome, IE, etc., Safari presents issues. My content div is positioned just below the header, but in Safari, it o ...

Regular expression for identifying a specific attribute paired with its corresponding value in a JSON object

Below is a JSON structure that I am working with: 'use strict'; // some comment is going to be here module.exports = { property1: 'value1', property2: 999, }; I am looking to remove the property2: 999, from the JSON. I attempted ...

Attempting to integrate a TypeScript library with a JavaScript Express application

I have been attempting to integrate a TypeScript library like this into an existing Express Node.js application, but unfortunately it is not working as expected. Upon importing the library functions, I keep encountering errors such as "Cannot read property ...

Tips for Resolving the Problem with React Hook Closures

import React, { useState } from "react"; import ReactDOM from "react-dom"; function App() { const [count, setCount] = useState(0); function handleAlertClick() { return (setTimeout(() => { alert("You clicked on: & ...

Trouble arises when attempting to animate the movement of two distinct objects consecutively using jQuery

I am facing an issue with combining animations of two different objects so that the second one starts when the first one ends. I attempted to use a callback function, but it seems I have made some syntax errors that are causing jQuery to crash or behave un ...

The function `splitPinCodes.split(',')` is causing a malfunction

I am encountering an issue with validating the input data. The input text area should contain 6-digit zip codes separated by commas. I have implemented the ng-change="convertToArray()" method in Angular for the input text area. If I enter more than 6 digi ...

Access a file from an npm module using an executable command

I have a npm module called @jcubic/lips, which includes an executable file. I need to open a file located within the module's directory. This module is installed globally. The specific file I want to access is ../examples/helpers.lips, relative to th ...

Prevent any sliding animations of content when the button is triggered

I've implemented a basic slideToggle functionality in jQuery. You can check out the code on JSFiddle here: $(document).ready(function() { $(".panel_button").on('click', function() { $(".panel").slideUp(); var targetPanel = $(thi ...

What steps can be taken to ensure the enter button (keyboard) functions correctly in forms on IE8?

The issue I'm facing is specific to Internet Explorer. While in any other browser I can easily submit the form by pressing enter on my keyboard, IE8 requires me to actually click the button. I've spent some time searching for solutions, but so f ...

Shaky xy positions while mapping a vector to NDC

Exploring ThreeJS has been an intriguing experience for me, especially when projecting vectors in the world space onto normalized device coordinates (NDC). Surprisingly, everything works flawlessly without any hiccups. However, a noticeable issue arises w ...

Creating a dynamic user interface with HTML and JavaScript to display user input on the screen

I'm currently working on creating an input box that allows users to type text, which will then appear on the screen when submitted. I feel like I'm close to getting it right, but there's a problem - the text flashes on the screen briefly bef ...

What is the best way to use setInterval with an Express application?

I've been searching online for a while, experimenting with different methods and coming up with unconventional solutions to my issue without making any progress. My main query is, how can I set an interval in my express.js application to run every 30 ...

What is the most efficient way to group elements in an Array by their shared property value and push them into a new Array as a single element?

Is there a way to group elements in an Array by their property value and put them into a new element, then push that new element into a new Array? data = [ {status: 0,name:'a'}, {status: 0,name:'b'}, {status: 1,name:'b ...

What is the best method for obtaining accurate normal values in three.js?

I'm having trouble understanding how normals are computed in Three.js. Here is the issue I am facing: I have created a simple plane using the following code: var plane = new THREE.PlaneGeometry(10, 100, 10, 10); var material = new THREE.MeshBasicMa ...

Ways to determine if the user is either closing the browser or navigating to a different website

I am looking to set up session management in a manner where all sessions are expired or destroyed when the user closes the browser or tab. However, I would like to retain all the sessions if the user is navigating to another website. Is there a way to ac ...

What is the best way to customize the color of the border and icon in an outlined MaterialUI input field

I'm a newcomer to materialUI and I have a question regarding customizing an outlined input field with an icon. Due to my dark background, I need the icon, border, and text color to be lighter, such as grey. Can someone please advise on the materialUI ...

What is the best way to remove double quotes surrounding "name" and "count" when displayed in the JavaScript console output?

I am struggling to transform the input: ["apple", "banana", "carrot", "durian", "eggplant", "apple", "carrot"] into the desired output: [{ name: "Apple", count: 2 }, { name: ...

Avoiding unnecessary re-renders in your application by utilizing the useRef hook when working with

To prevent the component from re-rendering every time the input value changes, I am trying to implement useRef instead of useState. With useState, the entire component re-renders with each key press. This is the usual approach, but it causes the entire co ...

You are unable to access the property outside of the callback function of the XMLHttpRequest

Currently, I am attempting to retrieve data from an XMLHttpRequest's onreadystatechange callback. Logging the data works perfectly fine within the callback function. However, when I try to use this data outside of the callback function with this.proce ...

Mastering the 'wing' corner effect using only CSS

Is there a way to achieve the corner effect shown in this image (top corners)? I'm not familiar with the name of this effect. If it is possible, how would you go about implementing it? Update: Some have suggested that this question is a duplicate. Ho ...