javascript get the current date and year

Examining the javascript code below:

<script type="text/javascript">
    $(function () {
        var currentDateTime = new Date();
        var oneYear = new Date();
        oneYear.setYear(oneYear.getYear() + 1);
        alert(currentDateTime + "_" + oneYear);
    });
</script>

The expected output of the alert should be the current datetime and the datetime one year from now. However, the actual result in the alert is: "Fri Oct 22 2010 14:17:31 GMT-0400 (Eastern Daylight Time)_Thu Oct 22 0111 14:17:31 GMT-0400 (Eastern Daylight Time)". This shows that instead of adding "1" to the Year correctly, it displays '0111'.

This raises the question: What exactly is happening here? How did the year turn into '0111'?

Answer №3

Swap out .getYear() for .getFullYear() instead

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

Using Typescript and react-redux with a Stateful Component: The parameter type 'typeof MyClass' does not match the expected component type

I've been trying to dive into the world of Typescript, React, and Redux. However, I've hit a roadblock at the moment. This is the error I encountered: ./src/containers/Hello.tsx [tsl] ERROR in /home/marc/Development/TypeScript-React-Starte ...

Utilizing JavaScript regex to eliminate multiple backslashes while maintaining the special character

To load JSON data with multiple backslashes before a newline character, we are utilizing JavaScript. An example of this is: { "test": { "title": "line 1\\\\\\\nline2" } } Various RegEx patterns have been ...

An AJAX event handling function returns a null value upon invocation

Recently, I've been working on a function named 'getAuthor' which includes an AJAX event. Here's the code snippet: function getAuthor(id){ $.get('http://www.connectnigeria.com/articles/wp-json/wp/v2/users/74',function(e){ ...

Can you explain how the functionality of setState operates within React in this specific situation?

I've been working on updating the state of an object in my array. I managed to get it done, but I'm a bit confused about how it works. toggleVisited = countryCode => { var countries = [ ...this.state.countries ]; var countryToChange = c ...

Having trouble with my loop using Jquery's $.get method

Having an issue with Jquery mobile / JavaScript I'm struggling to properly loop through the different values of data in my "get" function, where it should be sending ...cmd=PC&data=06464ff ...cmd=PC&data=16464ff ...cmd=PC&data=26464ff ...

What is the best way to use two buttons to sort the table's column type?

I need to sort the type column. There will be two buttons, one to filter aquatic animals and the other to filter all animals except aquatic ones. <button type="button" onclick="Aquatic()">Aquatic</button> <button type="button" onclick ...

Get a numerical value from a JSON object

Attempting to retrieve information from an API, but encountering an issue due to a numeric property name. The method for accessing the data is as follows: Success: data[i].price_usd Unsuccessful Attempt: data[i].24h_volume_usd Have experimented with ...

Modify content once it has been rendered using Jquery

After loading, I want to adjust the style of a specific element. However, I am running into a null pointer issue. function loadFriends() { $("#friendsContainer").load("friends.html"); } selectCurrentSetting(); function selectCurrentSetting() { c ...

`Why is my table row not appearing horizontally in Cordova/Phonegap?`

Why is my tr tag displaying buttons vertically instead of horizontally? A similar code works properly on jsfiddle What am I doing wrong? Here is my HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="form ...

Tips for repairing a button using a JavaScript function in an HTML document

I am currently working on extracting titles from body text. To achieve this, I have created a button and linked my function to it. The issue I am facing is that when I click on the button, it disappears from its original position. I intend to keep it in pl ...

Guide to Efficiently downloading a PDF file using Vue and Laravel

THE SCENARIO: Client Side: Vue. Server Side: Laravel. Within the web application, I need to enable users to download specific PDF files: I require Laravel to retrieve the file and send it back as a response to an API GET request. Then, in my Vue web ap ...

Strategies for extracting data from a third-party website that utilizes JavaScript to set the value

Before, I would use jQuery to load external website content such as html or json. Sometimes, I even utilized a proxy PHP page in order to bypass strict origin policies on certain sites. However, I've encountered an issue with some websites. In the HT ...

Concealing tooltip when button is clicked using jQuery

I am facing an issue where the tooltip does not hide on button click. Below is the code for the button in question: <button class="btn btn-outline-inverse ajax_addtocart additems ...

Having trouble getting the Facebook like button to display on my website using an iframe in the markup

I gave it my all to try and get it to work, but unfortunately, I was unsuccessful. This is the approach I took. First, I followed the instructions provided on https://developers.facebook.com/docs/plugins/like-button. Next, I copied and pasted the iframe ...

Choosing specific information from a deeply nested JSON structure

I am currently dealing with a multi-level JSON object that includes an array of 143 other objects. When I run console.log(obj) on this object, the following output is displayed: 0: Object ActFTEs: 0.00 Actual: 11111 Bud_Month: "October" FY_CD: 201 ...

Can the parent of a <div> be modified with JavaScript?

Is there a way to dynamically change the parent of a div element? Here is an example scenario: <body> <div id="div_a"> <div id="child"></div> </div> <div id="div_b"> </div> </body> I am looking ...

What is the method to retrieve a checkbox value within a datatable when employing pagination?

I recently came across an issue with my datatable where I had implemented a checkbox in the first column of each row. However, when I attempted to check the checkbox using an AJAX call, it only worked for the first page and not for any subsequent pages. H ...

Is there a way to trigger a Modal to open upon clicking a custom-designed button in MaterialUI?

In my React and Material-UI project, I am attempting to trigger a Modal to open using a custom button. The button also performs other functions, which is why it's important for me to combine the "Modal Opening" action with these additional functionali ...

Assign a variable to set the property of a class

Could something similar to this scenario be achievable? const dynamicPropName = "x"; class A { static propName = 1 // equivalent to static x = 1 } A[dynamicPropName] // will result in 1 or would it need to be accessed as (typeof A)[dynamicPropN ...

Add transparency to the background color when hovering over inline elements

Initially, my style sheet had the following structure: .button { border-radius: 3px; display: inline-block; padding: 14px 20px; transition: background-color cubic-bezier(0.4, 0.0, 0.2, 1.0) 300ms; &:hover { background-color: transparent ...