-1
$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 -->

Hi, I currently have this. So the date shows as May 15, 2008. How do I show only the year or only the year+month?

Thanks.

Jason
  • 11
  • 5

2 Answers2

3

According to the documentation, you can use date.getFullYear() to get the year in YYYY format and date.getMonth() to get the month.

An example:

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


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

list.appendChild(yearNode)

// year + month

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

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

</ul>
Rick van Lieshout
  • 2,118
  • 1
  • 17
  • 38
Fire
  • 383
  • 1
  • 7
  • Hi, I tried `"date": new Date('2008, 4, 15').getFullYear()` but it is returning December 31, 1969 – Jason Apr 11 '18 at 21:07
  • either remove the ' or add dashes between the items. `new Date('2018-4-15').getFullYear()` will work fine. – Rick van Lieshout Apr 12 '18 at 08:19
  • Thanks, I forgot to mention I have this `{{article.date | date:'longDate'}}` on the main page. When I removed `date:'longDate'` and added `"date": new Date('2008, 4, 15').getFullYear()` it worked so these two seem to be conflicting. However, now that I've removed the `longDate` format the dates are not formatted correctly anymore. Edited my original post. – Jason Apr 12 '18 at 16:02
0

with date.getMonth() you need +1 to this var month = date.getMonth() + 1

Demon Spear
  • 106
  • 4