The current date object in JavaScript will only display the year or a combination of the month and

$scope.articles = [
    {
    link: "http://google.com",
    source: "Google",
    title: "hello",
    "date": new Date(2008, 4, 15)
    },
];

<tbody>
    <tr ng-repeat = "article in articles | orderBy:sortType:sortReverse | filter:searchArticle ">
        <td>{{article.source}}</td>
        <td><a href="{{article.link}}" target="_blank" rel="noopener noreferrer">{{article.title}}</a></td>
        <td class="date-table-td">{{article.date | date:'longDate'}}</td>
    </tr>
</tbody><!-- End table body -->

Hey there! I've got this code snippet and it's displaying the date as May 15, 2008. Do you know how I can modify it to show just the year or maybe only the year and month?

Appreciate any help with this. Thank you!

Answer №1

The instructions provided in the resource state that using date.getFullYear() will retrieve the year in YYYY format, while date.getMonth() will fetch the month.

For illustration purposes:

let list = document.getElementById('test');
let date = new Date(2008, 4, 15);

// Extracting the year
let yearNode = document.createElement("li");
yearNode.appendChild(document.createTextNode(date.getFullYear()));

list.appendChild(yearNode)

// Combining year and month

let yearMonthNode = document.createElement("li");
yearMonthNode.appendChild(document.createTextNode(date.getFullYear() + " " + date.getMonth()))

list.appendChild(yearMonthNode)
<ul id="test">

</ul>

Answer №2

When using date.getMonth(), remember to add +1 to it like so: var month = date.getMonth() + 1

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

Transforming a collection of items into a JSON format string

UPDATE: There was a mistake in the programming, please refrain from submitting answers. This question will be removed. If you have already submitted an answer, kindly delete it. I am attempting to submit a form using jQuery and ajax. One of the fields con ...

Tips for setting up Highcharts tooltip.headerFormat using the function getDate() plus 5

I'm facing a little challenge trying to understand how the JavaScript function getDate interacts with Highcharts datetime on xAxis. My goal is to show two dates in the tooltip header, forming a date range like this: 1960/1/1 - 1965/1/1. The first da ...

Tips for Integrating a Facebook Shop Page into Your Website

Can a Facebook shop page be integrated into a website? Any guidance on how to accomplish this task would be greatly valued. ...

What is the best way to populate dropdown menus using JavaScript?

I'm facing an issue with my ajax request where I am unable to populate the options of a select tag. This problem is occurring in multiple blocks where the select tag serves the purpose of choosing a type of product. Here is how my select tag looks li ...

Passing slots to child components within a VueJS application - A helpful guide

Within my application, I frequently utilize a list and list_item component. To provide a general idea: contact_list.vue <template lang="pug"> .table .table-header.table-row .table-col Contact .table-col Info .tabl ...

Tips on transforming two same-length arrays into a single array of objects using JavaScript

I have a dilemma with two arrays that have been structured as follows: arr1 = [10, 20, 30, 40, 50]; arr2 = ['x', 'y', 'z', 'w', 'v']; My goal is to utilize JavaScript in order to transform these arrays of ...

JavaScript's Array.map function failing to return a value

Here is a snippet of code from an api endpoint in nextJS that retrieves the corresponding authors for a given array of posts. Each post in the array contains an "authorId" key. The initial approach did not yield the expected results: const users = posts.ma ...

Trouble with displaying days on the Datepicker

I'm currently facing an issue with my datepicker. I have a specific set of days that should be highlighted on the calendar, and it needs to be constantly updated. For example, past days are removed from the calendar automatically to keep it current. H ...

Reorganizing items within an ArrayCollection using RESTAPI

Consider this scenario: I have an array collection called $items, and I have implemented a method called removeItem in my SomeEntity entity class, public function removeItem(Item $item) { $this->items->removeElement($item); return $this; } ...

Run the jQuery script once it has been successfully loaded using jQuery's .load method

On the Index.html page, there is a select box labeled #choose_content_to_load and a div named #get_loaded_content <select id="choose_content_to_load"> <option>Some content from the page content.html and div #content</option> </select ...

When choosing an input field, it consistently yields an undefined value

Whenever I attempt to select an option in the input field, it should set the state value to the selected option. However, it keeps returning undefined. I am utilizing Semantic UI React Forms for input, but every time I select an option and submit, it retu ...

Issue with error handling in Node and MongoDB when using Express, Mongoose, and the 'mongoose-unique-validator' plugin

I am facing an issue with the 'mongoose-unique-validator' plugin when trying to handle Mongo ValidationError in my custom error handler. Despite other errors being handled correctly, this specific one is not triggering the desired response from m ...

Modifying the default label for each bubble on a bubble chart with chartjs-plugin-datalabels

Is there a way to add labels to each bubble in the bubble chart using chartjs-plugin-datalabels? For every bubble, I'd like to display the label property of each object within the data.dataset array, such as "Grapefruit" or "Lime". Currently, I'm ...

When copying text from React-PDF Display, the values may appear altered or varied

This snippet of text was excerpted from a brief SHA provided at the git summit. Generated using React-pdf, this is a PDF document with some interesting quirks. Although the displayed text reads as 4903677, it changes to •G07THH when copied. The font ...

"JavaScript issue: receiving 'undefined' when trying to retrieve input

This code snippet is for a web app that tracks the number of losses in a game. The problem arises when trying to retrieve the value, which returns undefined. Every time I reference the username variable, it returns undefined. document.addEventListener(&a ...

Initial space in model variable not being displayed

Hey there, I am working with a model variable called "name" that is linked to a span element like this: <input type="text" ng-model="name"> <span ng-bind="name"></span> My goal is to display the text entered in the input field without r ...

Prevent form submission from refreshing the page by using jQuery and ajax

I'm looking to submit a form without refreshing the page, but my current script isn't working as expected: $('#formId').submit(function(e){ $.ajax({ type: "POST", url: 'serverSide.php', data: $(&ap ...

Displayed in the xmlhttp.responseText are the tags

Why do tags appear when I input xmlhttp.responseText into my textbox? It displays <!DOCTYPE html><html><body></body></html> along with the desired content. Is there a way to prevent the tags from being displayed? Here is the ...

"Facing a dilemma with Javascript's Facebox getElementById function - not fetching any

Utilizing facebox, I am initiating a small modal dialog with a form and attempting to retrieve the value from a text field on that form using javascript. Below is the HTML code: <div id="dialog-form95" style="display:none"> <div class="block"> ...

Do not begin the next task until the current function has properly concluded

I am currently developing a project in Ionic v4 with Angular that involves using BLE to communicate with a Raspberry Pi. The project consists of several steps: Searching for devices around me Connecting to the desired device Activating notifications Sen ...