javascript - new Date() only show the year or month and year - Stack Overflow

$scope.articles = [{link: "",source: "Google",title: "hello","date&q

$scope.articles = [
    {
    link: "",
    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.

$scope.articles = [
    {
    link: "http://google.",
    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.

Share Improve this question edited Apr 12, 2018 at 16:04 Jason asked Apr 11, 2018 at 18:46 JasonJason 211 silver badge6 bronze badges 3
  • 3 Possible duplicate of How to format a JavaScript date – Alexander Lallier Commented Apr 11, 2018 at 18:49
  • check Date.prototype – Sphinx Commented Apr 11, 2018 at 18:49
  • 2 Actually new Date(2008, 4, 15) should show up as May, not March. – kshetline Commented Apr 11, 2018 at 18:50
Add a ment  | 

2 Answers 2

Reset to default 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>

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745121769a4612455.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信