简体   繁体   English

如何显示以前的日期,直到今天和聚合物的几年的下一个日期

[英]How to show previous dates till today and next dates for some years on polymer

How to show previous dates till today and next dates for some years on polymer? 如何显示聚合物的前几天到今天和接下来的几年?
I tried the below script but it was not working. 我尝试了以下脚本,但它无法正常工作。
I am getting only yesterday's and tomorrow's dates on console on repeat. 我只在昨天和明天的日期在控制台上重复。 I want the day before then the one before and so on. 我希望前一天前一个,依此类推。 Also I must not go behind my present day. 我也不能追溯到今天。

<header>
  <iron-icon class="icon" icon="chevron-left" on-tap="previousDay" suffix></iron-icon>
  <iron-icon class="icon" icon="chevron-right" on-tap="nextDay" suffix></iron-icon>
</header>
<br/><br/>       



nextDay: function() {
  var currentDay = new Date(); 
  currentDay.setDate(currentDay.getDate() + 1);
  console.log(currentDay);
},

previousDay: function () {
  var currentDay = new Date();
  currentDay.setDate(currentDay.getDate() - 1);
  console.log(currentDay);
},

A possible solution would be: 一个可能的解决方案是:

nextDays: function() {
    var currentDay = new Date();
    var nextDays = [];

    for (var i = 1; i <= 30; i++) {
        var newDate = new Date();
        newDate.setDate(currentDay.getDate() + i);
        nextDays.push(newDate);
    }

    // nextDays will contain the 30 next days
}

previousDays: function() {
    var currentDay = new Date();
    var previousDays = [];

    for (var i = 1; i <= 30; i++) {
        var newDate = new Date();
        newDate.setDate(currentDay.getDate() - i);
        previousDays.push(newDate);
    }

    // previousDays will contain the 30 last days
}

You can simply change the number of days you want (30 in this example) and do what you want with the arrays nextDays and previousDays . 您可以简单地更改所需的天数(在此示例中为30天),并使用数组nextDayspreviousDays执行您想要的nextDays

What you can add is for example cache these days so that you don't have to generate them everytime (especially if its for the same day). 你可以添加的是这些天的缓存,这样你就不必每次都生成它们(特别是如果它在同一天)。

Here is a simple solution using private variables. 这是一个使用私有变量的简单解决方案。

 var date = (() => { var currentDay = new Date(); function previousDay() { currentDay.setDate(currentDay.getDate() - 1); return currentDay; } function nextDay() { currentDay.setDate(currentDay.getDate() + 1); return currentDay; } return { previousDay, nextDay }; })(); 
 <button onclick="console.log(date.previousDay())">Previous day</button> <button onclick="console.log(date.nextDay())">Next day</button> 


You can even shorten it, both can use the same function: 你甚至可以缩短它,两者都可以使用相同的功能:

 var date = (() => { var currentDay = new Date(), previousDay = () => goToDay(-1), nextDay = () => goToDay(1); function goToDay(n) { currentDay.setDate(currentDay.getDate() + n); return currentDay; } return { previousDay, nextDay }; })(); 
 <button onclick="console.log(date.previousDay())">Previous day</button> <button onclick="console.log(date.nextDay())">Next day</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM